CPP program for implement string pattern matching
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class
string
{private:
char s1[100],s2[100];
int l1,l2,i;
public:
void readstring();
void displaystring();
void compare();
};
void
string:: readstring()
{ cout<<"\nEnter
the first string ";
gets(s1);
cout<<"\nEnter the second string ";
gets(s2);
}
void
string:: displaystring()
{ cout<<"\nThe first
string is \n " <<s1;
cout<<"\nThe second string is \n " <<s2;
}
void
string:: compare()
{
l1=l2=0;
for(i=0;s1[i]!='\0';i++)
l1++;
for(i=0;s2[i]!='\0';i++)
l2++;
if(l1>l2)
cout<<s1<<"
is greater than "<<s2;
else
if(l1<l2)
cout<<s1<<"
is less than "<<s2;
else
{ int
m=0,n=0;
cout<<"Strings are
of same length\n";
for(i=0;i<l1;i++)
{ s1[i]=(s1[i] >='a'&&s1[i]
<'z')?('A'+s1[i]-'a'):s1[i];
s2[i]=(s2[i]
>='a'&&s2[i] <'z')?('A'+s2[i]-'a'):s2[i];
if(s1[i] <s2[i])
m++;
else
if(s1[i]
>s2[i])
n++;
}
if(n==0&&m==0)
cout<<"\n and
Strings are of same data\n";
else
cout<<"\n
but, Strings are of not same data\n";
}
}
void
main()
{ int c;
char
ch;
string
s;
while(1)
{ clrscr();
cout<<"Perform
the following operations "<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"1.Enter
the strings \n";
cout<<"2.Display
the strings \n";
cout<<"3.Compare
the strings \n";
cout<<"4.Exit from Program
\n";
do
{ cout<<"Select your
choice(1:4)";
cin>>c;
}
while((c<1)||(c>4));
if(c==4)exit(1);
clrscr();
switch(c)
{case
1:
s.readstring();
break;
case
2:
s.displaystring();
break;
case
3:
s.compare();
break;
}
cout<<endl<<"Press
any key to continue...";
getch();
}
}
No comments:
Post a Comment