Answer to Question #188880 in C++ for Asad

Question #188880

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.

 


1
Expert's answer
2021-05-12T18:01:15-0400
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS