Tuesday, 20 October 2015

Check whether two strings are anagram of each other

#include<bits/stdc++.h>
using namespace std;
int main(){
    string a,b;
    int i;
    int ha[124]={0};
    int hb[124]={0};
    cout<<"enter the two strings\n";
    cin>>a>>b;
    if(a.length()==b.length()){
        for(i=96;i<a.length();i++){
            ha[a[i]]++;
        }
        for(i=96;i<b.length();i++){
            hb[b[i]]++;
        }
        for(i=96;i<124;i++){
            if(ha[i]!=hb[i]){
                break;
            }
        }
        if(i==124){
            cout<<"Yes both the strings are anagram of each other.\n";
        }
        else{
            cout<<"No two strings are not anagram of each other.\n";
        }
    }
    else{
        cout<<"No two strings are not anagram of each other.\n";
    }
    return 0;
}

No comments:

Post a Comment