Showing posts with label Permutation. Show all posts
Showing posts with label Permutation. Show all posts

Friday 6 July 2012

Write a method to decide if two strings are premutation of each other or not.


#include<stdio.h>
#include<string.h>
int check_permutation(char s1[], char s2[])
{
    int c[27],i;
    for(i=0;i<27;i++)
        c[i]=0;
    if(strlen(s1)!=strlen(s2))
        return 0;
    for(i=0;s1[i]!=NULL;i++)
    {
        c[0]= s1[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]++;

        c[0]= s2[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]--;
    }
    for(i=1;i<27;i++)
    {
        if(c[i]!=0)
            return 0;
    }
    return 1;
}

int main()
{
    char s1[20],s2[20];
    scanf("%s%s",&s1,&s2);
    if( check_permutation(s1,s2) )
       printf("\n Permutation of each other");
    else
        printf("\n NOT a Permutation ");
return 0;
}

Wednesday 4 July 2012

WAP to return all anagrams from the array of strings. (all possible permutation of a String)


#include<stdio.h> #include<string.h> int anagram(char S[10],int l,char C[10]) { char D[10]; int j,k,m=strlen(C),i; for(j=0;j<=m;j++) { for(k=m;k>j;k--) D[k]=C[k-1]; D[k]=S[m]; D[m+1]=NULL; for(k=j-1;k>=0;k--) D[k]=C[k]; if((m+1)==l) printf("%s\n",D); else anagram(S,l,D); } return 0;
} int main() { char S[10],C[10]; C[0]=NULL; gets(S); printf("\n"); anagram(S,strlen(S),C); return 0; }