Programming Project : Anagrams You are to write a program that tests whether two
ID: 3638977 • Letter: P
Question
Programming Project : Anagrams
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 chappears 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.
Then the two words are anagrams if and only if the count array contains only zeros.
Concepts being tested: arrays, loops, character input.
Explanation / Answer
#include #include #include int main() { char an1[30],an2[30]; int check[26]={0},i; printf("Enter 1st phrase: "); scanf("%s",&an1); printf("Enter 2nd phrase: "); scanf("%s",&an2); for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.