Write a program called anagram.c that asks the user for 2 words and tells the us
ID: 640075 • Letter: W
Question
Write a program called anagram.c that asks the user for 2 words and tells the user if those 2 words are anagrams. A word is an anagram of another word if the letters in that word can be rearranged to form the other word. For example Mary and army are anagrams.
Name your executable anagram.out
The user will only enter alphabetical characters
The maximum length of each word is 20 characters
The check for an anagram should be case insensitive. For example MARY and
army are still anagrams
Examples
Please enter the first word: MaRy Please enter the second word: arMY MaRy is an anagram of arMY
Please enter the first word: dog Please enter the second word: god dog is an anagram of god
Please enter the first word: bob Please enter the second word: bobs bob is NOT an anagram of bobs
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main(){
char buff[256], buff1[256];
char second[256], second1[256];
int arr[26] = {0};
int i = 0, flag = 1;
printf("Please enter the first word: ");
scanf("%s", buff);
strcpy(buff1, buff);
for(i = 0; i < strlen(buff); ++i){
if(buff[i] >= 97 && buff[i] <= 122)
buff[i] = buff[i] - 32;
arr[buff[i] - 'A']++;
}
printf("Please enter the second word: ");
scanf("%s", second);
strcpy(second1, second);
for(i = 0; i < strlen(second); ++i){
if(second[i] >= 97 && second[i] <= 122)
second[i] = second[i] - 32;
arr[second[i] - 'A']--;
}
for(i = 0; i < 26; ++i){
if(arr[i] != 0){
flag = 0;
break;
}
}
if(flag == 1){
printf("%s is an anagram of %s", buff1, second1);
}
else{
printf("%s is NOT an anagram of %s", buff1, second1);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.