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

Write a program called anagram.c that asks the user for 2 words and tells the us

ID: 3696894 • 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 tonum(char ch)
{
if(ch>='a' && ch<='z')
return ch-'a';
return ch-'A';
}
int main()
{
char first[20],second[20];
printf("Enter the first word:");
scanf("%s",first);
printf("Enter the second word:");
scanf("%s",second);
int ch[26];
int i,j,k;
int flag=0;
for(i=0;i<26;i++)ch[i]=0;

for(i=0;i<strlen(first);i++)ch[tonum(first[i])]++;
for(i=0;i<strlen(second);i++)ch[tonum(second[i])]--;

for(i=0;i<26;i++)
{
if(ch[i]!=0)
{
flag=1;
break;
}
}
if(flag==0)
printf("Both the strings are anagrams ");
else
printf("Both the strings 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