You have to take two strings input from user and compare then in such a way that first index of string 1 should be compared second index of string 2 and second index of string 1 should be compared with first index of string 1, third index of string 1 should be compared with the fourth index of string 2 and fourth index of string 1 should be compared with the third index of string 2 and so on……..
a b c d f g
a j c j c n
If comparison is successful then swap the consecutive two elements of string 2 and compared the swapped string 2 with string 1 using built-in strcmp() and print the swapped string otherwise print not equal.
#include<bits/stdc++.h>
using namespace std;
int compare(char s1[],char s2[])
{
int i,f;
if(!strcmp(s1,s2) || strlen(s1)%2!=0)
{
return 0;
}
else
{
for(i=0;i<strlen(s1);i=i+2)
{
if(s1[i]==s2[i+1] && s1[i+1]==s2[i])
{
f=0;
}
else
{
f=1;
break;
}
}
}
if(f==1)
{
return 0;
}
else
{
return 1;
}
}
void swapp(char s1[],char s2[])
{
char t;
if(compare(s1,s2)==1)
{
for(int i=0;i<strlen(s2);i=i+2)
{
t=s2[i];
s2[i]=s2[i+1];
s2[i+1]=t;
}
cout<<"\nSwapped string : "<<s2;
}
else
{
cout<<"\nNot Equal ";
}
}
int main()
{
char s1[40],s2[40];
cout<<"Enter first string : ";
cin>>s1;
cout<<"Enter second string : ";
cin>>s2;
swapp(s1,s2);
}
Comments
Leave a comment