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;
}

Program to check if 2 strings are rotations of each other or not

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cout<<"enter the first string\n";
    cin>>a;
    cout<<"enter the second string\n";
    cin>>b;
    if(a.length()==b.length()){
        string c=a+a;
        if(c.find(b)){
            cout<<"Yes , both strings are rotation of each other\n";
        }
    }
    else{
        cout<<"No, both strings are not the rotation of each other\n";
    }
    return 0;
}

Program to reverse the order of words in the entered string

#include<bits/stdc++.h>
using namespace std;
int main(){
    char str[10000];
    string s[20];
    cout<<"Please enter the string\n";
    gets(str);
    strrev(str);
    int cnt1=0,cnt2=0;
    for(int i=0;i<strlen(str);i++){
        if(str[i+1]=='\0'){
            for(int j=i;j>=cnt1;j--){
                cout<<str[j];
            }
            cout<<"\n";
        }
        if(str[i]==' '){
            for(int j=i-1;j>=cnt1;j--){
                cout<<str[j];
            }
            cout<<" ";
            cnt1=i+1;
        }
    }
    return 0;
}

Program to reverse the entered string using recursion

#include<bits/stdc++.h>
using namespace std;
void printstr(char *s)
{
    if(*s)
    {
        printstr(s+1);
        printf("%c",*s);
    }
}
int main()
{
    int index;
        char a[1000];
        int mx=0;
        int ar[1000]= {0};
        cin>>a;
        printstr(a);
    return 0;
}

Program to output the entered string with duplicates removed

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int index;
        string a,b="";
        int mx=0;
        int ar[1000]= {0};
        cin>>a;
        for(int i=0;i<a.length();i++)
        {
            ar[a[i]]=1;
        }
        for(int i=0;i<a.length();i++)
        {
            if(ar[a[i]]!=0){
                b+=a[i];
                ar[a[i]]=0;
            }
        }
        cout<<b<<endl;
    return 0;
}

Program to return the character with maximum frequency in the entered string

//MADE BY KRISHNA AGARWAL
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int index;
        string a;
        int mx=0;
        int ar[1000]= {0};
        cout<<"Please enter the string\n";
        cin>>a;
        for(int i=0; i<a.length(); i++)
        {
            ar[a[i]]++;
        }
        index=0;
        for(int i=0; i<100; i++)
        {
            if(ar[i]!=0){
                if(mx<ar[i]){
                    mx=ar[i];
                    index=i;
                }
            }
        }
        cout<<"maximum occuring character in the entered string \n";
        cout<<char(index)<<" "<<mx<<endl;
    return 0;
}