Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to write a program that tests whether two words are anagrams (permutatio

ID: 3639087 • Letter: Y

Question

You are to write a program that tests whether two words are anagrams (permutations of the same letters). Non-alphabetic characters are ignored and lowercase characters are treated as the corresponding upper case character.

In the output of the sample executions of the program shown below, the user input is underlined (so your program does not print these!)

Output of Sample Execution 1:
Enter first word: matTress Enter second word: smarteSt The words are anagrams

Output of Sample Execution 2:
Enter first word: dumbest Enter second word: stumble The words are not anagrams

As usual, a word is entered letter-by-letter and then the user hits the return key, so the input is terminated by a newline character.
You should use an integer array count of length 26 to keep track of the number of times each alphabetic character appears in the first word. You should #include <ctype.h> which provides the functions isalpha, which tests a character to see that it is alphabetic, and toupper, which converts an alphabetic character to its uppercase form. As for the count array, count[0] should contain the number of ‘A’s in the word, count[1] the number of ‘B’s, … . For an arbitrary uppercase letter ch, count[ch-‘A’] should contain the number of times ch appears in the word.
So, you should write a loop that reads in the first word, character by character, until the newline is encountered. For each alphabetic character, you increment its entry in the count array. Then you write another loop that reads in the second word, character by character, and decrement the count for each such character.
T

hen the two words are anagrams if and only if the count array contains only zeros.
All your work must be in the file named anagrams.c.
Concepts being tested: arrays, loops, character input.

Explanation / Answer

#include
#include

#define N 26

int main()
{
char ch;
int letter1[N] = {0};
int letter2[N] = {0};
int i;


printf("Enter a word: ");
ch = 'A';
if(isalpha(ch))
ch = tolower(ch);



while ((ch = getchar())!= ' ')
{
letter1[ch -'a']++;
}
for (i = 0; i < N; i++)
{
}

printf("Enter a second word: ");

while ((ch = getchar())!= ' ')
{
letter2[ch -'a']--;
}
for (i = 0; i < N; i++)
{
}

for(i = 0; i < N; i++)
{
if (letter1[i] == 0 && letter2[i] == 0)
{
printf("The words are anagrams ");
} else {
printf("The words are not anagrams ");
}
}
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote