C++ programming language- part- 85 Programming Logic and Technique:
Ex: 21_2 Compare two string, the string is equal or not.
Programming
Logic and Technique:
//compare two strings (arraydemo.c)
char
string1[10],string2[10];//array of size 10 defined
int i;
char K=1;
cout<<"type
first string "<<"\n";
cin>>string1;
cout<<"type
second string "<<"\n";
cin>>string2;
for(i=0;string1[i]!='\0',string2[i]!='\0';i++);
{
if(string
1[i]!= string 2[i])
{
k=0;
}
}
if(k==0)
{
cout<<
"those are not equal ";
}
else
{
cout<<"
those are equal ";
}
}
Program
Ex_21_2
//compare two strings (arraydemo.c)
#include
#include
void main()
{
clrscr(); // To clean the screen
char
a1[10],a2[10];//array of size 10 defined
int i;
char flag=1;
cout<<"type
first string "<<"\n";
cin>>a1;
cout<<"type
second string "<<"\n";
cin>>a2;
for(i=0;a1[i]!='\0',a2[i]!='\0';i++);
{
if(a1[i]!=a2[i])
{
flag=0;
}
}
if(flag==0)
{
cout<<
"those are not equal ";
}
else
{
cout<<"
those are equal ";
}
}
Program Ex_21_3
//assign first array to other (arraydemo.c)
#include
#include
void main()
{
clrscr(); // To clean the screen
char
a1[5],a2[5];//array of size 5 defined
int i,j;
for(i=0;i<5 i="" o:p="">5>
{
cout<<"type
number/char "<<"\n";
cin>>a1[i];
a2[i]=a1[i];
}
cout<<"
The second array is "<
//cin>>a2[i];
for(i=0;i<5 i="" o:p="">5>
cout<
}
Program Ex_21_4
//palindrome
(arraydemo.c)
#include
#include
void main()
{
clrscr(); // To clean the screen
char
a1[10],a2[10];//array of size 10 defined
char a3[20]; //
can be char *a3
int i,j;
//a3=new char[20];
cout<<"type the string "<<"\n";
cin>>a1;
cout<<"type
another string "<<"\n";
cin>>a2;
for(i=0;a1[i];i++)
a3[i]=a1[i];
for(j=0;a2[j];j++,i++)
a3[i]=a2[j];
a3[i]='\0';
cout<<"The
marged string is "<
}
Alternate
#include
void main()
{
clrscr(); // To clean the screen
char
a1[10],a2[10];//array of size 10 defined
int i,j;
cout<<"type the string1 "<<"\n";
cin>>a1;
cout<<"type
another string2 "<<"\n";
cin>>a2;
for(i=0;a1[i]!='\0';i++);
for(j=0;a2[j]!='\0';j++,i++)
{
a1[i]=a2[j];
}
a1[i]='\0';
cout<<"the
content is "<
}