How to check if two String are Anagram?

Asked 27-Feb-2018
Viewed 365 times

1 Answer


0

Anagrams String :
 A string is called anagram string if another string contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.
To check whether two Strings are Anagrams String we need to follow these step -
Step-1. First be take two strings as input.
Step-2. Sort two strings according to their ASCII values.
Step-3. Now compare the two strings. If both are equal, then they are anagrams. Otherwise they are not anagrams.
Example :

#include <stdio.h>


int find_anagram(char [], char []);

int main()
{
    char array1[100], array2[100];
    int flag;

    printf("Enter the string\n");
    gets(array1);
    printf("Enter another string\n");
    gets(array2);
    flag = find_anagram(array1, array2);
    if (flag == 1)
        printf(""%s" and "%s" are anagrams.\n", array1, array2);
    else
        printf(""%s" and "%s" are not anagrams.\n", array1, array2);
    return 0;
}

int find_anagram(char array1[], char array2[])
{
    int num1[26] = {0}, num2[26] = {0}, i = 0;

    while (array1[i] != '\0')
    {
        num1[array1[i] - 'a']++;
        i++;
    }
    i = 0;
    while (array2[i] != '\0')
    {
        num2[array2[i] -'a']++;
        i++;
    }
    for (i = 0; i < 26; i++)
    {
        if (num1[i] != num2[i])
            return 0;
    }
    return 1;
}