String Comparision Using Bubble Sort



1.    Write a program to find whether two given strings are permutations of each other.  A string str1 is a permutation of str2 if all the characters in str1 appear the same number of times in str2 and str2 is of the same length as str1.

Input: Two strings S1 and S2
Output:
yes - if they satisfy given criteria
no - otherwise

Constraints:
1 <= len(S1), len(S2) <= 100.
Characters from ASCII range 0 to 127.
White space will not be given in the string.




#include<iostream>
#include<string>
using namespace std;

string  asc(string str)
    {
    int i,j,temp;
   for(i=0;str[j]!='\0';i++)
    {
        for(j=i+1;str[j]!='\0';j++)
        {
            if(str[j]<str[i])
            {
                temp=str[j];
                str[j]=str[i];
                str[i]=temp;
            }
        }
    }
    return str;
    }


    int stringCompare(string str1,string str2)
    {
    int i,flag=0;

    for(i=0;str1[i]!='\0' && str2[i]!='\0';i++)
        {
         if(str1[i]!=str2[i])
            {
             flag=1;
             break;
            }
        }

    if (flag==0 && str1[i]=='\0' && str2[i]=='\0')
         return 1;
    else
         return 0;

}

int main()
{
    string s1,s2;
    int res;
    cout<<"Enter the string 1:";
    cin>>s1;
    s1=asc(s1);
    cout<<"Enter the string 2:";
    cin>>s2;
    s2=asc(s2);
    res=stringCompare(s1,s2);
    if(res==0)
        cout<<"\nYes";
    else
        cout<<"No";
}

No comments:

Post a Comment

Thank you for using this blog.