C problem solution:Anagrams

Anagram:

#include<stdio.h>
int check_anagram(char [],char []);
int main()
{
    char a[100],b[100];
    int flag;

    printf("Enter first string\n");
    gets(a);
    printf("Enter second string\n");
    gets(b);

    flag=check_anagram(a,b);
    if(flag==1)
        printf("%s and %s are anagrams\n",a,b);
    else
        printf("%s and %s are not anagrams",a,b);
    return 0;
}

int check_anagram(char a[],char b[])
{
    int first[26]={0};
    int second[26]={0};
    int c=0;

    while(a[c]!='\0')         //ei jaygar logic ta pore bujhte hobe
    {
        first[a[c]-'a']++;      //ei jaygataro
        c++;
    }
c=0;
while(b[c]!='\0')            //ei jaygar logic ta pore bujhte hobe
{
    second[b[c]-'a']++;          //ei jaygataro
    c++;
}

for(c=0;c<26;c++)
{
    if(first[c]!=second[c])
        return 0;
}

return 1;
}

Output:

Enter first string
zaki
Enter second string
kiza
zaki and kiza are anagrams

Process returned 0 (0x0)   execution time : 8.134 s
Press any key to continue.

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *