Design a C program to check whether the given strings are Is a Anagram or is not an Anagram".
#include <stdio.h>
int isAnagram(char string1[], char string2[]);
//The start point of the program
int main()
{
char string1[100];
char string2[100];
//get first string
printf("Enter string 1: ");
gets(string1);
//get second string
printf("Enter string 2: ");
gets(string2);
//Display result
if (isAnagram(string1, string2)){
printf("The strings are anagram\n");
}else{
printf("The strings are not anagram.\n");
}
getchar();
getchar();
return 0;
}
//This function checks if the two string are anagram
int isAnagram(char string1[], char string2[])
{
int string1Counter[26] = {0};
int string2Counter[26] = {0};
int index=0;
while(string1[index] != '\0') {
string1Counter[string1[index]-'a']++;
index++;
}
index = 0;
while (string2[index] != '\0') {
string2Counter[string2[index]-'a']++;
index++;
}
for (index = 0; index < 26; index++){
if (string1Counter[index] != string2Counter[index]){
return 0;
}
}
return 1;
}
Comments
Leave a comment