Write a C function to determine if one string is an anagram of another. Your fun
ID: 3575649 • Letter: W
Question
Write a C function to determine if one string is an anagram of another. Your function if anagram() will take two strings as parameters, and will returns 1 if the strings are anagrams of each other, otherwise it will returns 0. An anagram is a phrase that uses all the letter-and exactly those letters-of some other phrase. Below are some anagram examples: Zastre: Ersatz Stephen Harper: Eh! Rent, perhaps? Dormitory: Dirty Room The check is in the mail!: Claim 'Heck, I sent it (heh)" I sat there with Sally: Aha! Lithely twisters! Notice the number of spaces and use of punctuation in an anagram can differ gready from the original phrase. We ignore spaces and punctuation when checking if two phrases are anagrams of each other. As a guide towards a solution, assume you can use an already-written function named alpha_to index which takes a single parameter (a character). It returns 0 if the character is not an alphabetical character, otherwise it returns a value from 1 to 26 if the character is a letter from the English alphabet (When the function is passed 'A' or 'a' as the argument, it returns I; when 'B' or 'a' is passed, it returns 2; etc.) The next page (page IS) is available for your work/answer if needed. Some marks will be given for the quality of your answer.Explanation / Answer
#include <stdio.h>
int anagram(char [], char []);
int main()
{
char a[100], b[100];
int flag;
printf("Enter first string ");
gets(a);
printf("Enter another string ");
gets(b);
flag = anagram(a, b);
if (flag == 1)
printf(""%s" and "%s" are anagrams ", a, b);
else
printf(""%s" and "%s" are not anagrams ", a, b);
return 0;
}
int anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
while (a[c] != '')
{
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != '')
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return 0;
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.