The program prompts the user to type two words stored in two different arrays. If the words are anagram it would print "Anagram", if not it'd print "Not Anagram". I made an array for all the alphabet letters, letter 'a' is stored as {1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...} filling in he whole array of letters.
Then I compared both arrays, to determine if they are the same word I substracted each letter and if they are 0 (cancel out each other) they are Anagrams. Here's my code so far, I'm not sure what I'm doing wrong. I'm pretty sure there's something wrong in the Boolean function.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
void read_word(int counts[26])
{
int i;
char ch;
printf("Enter a word: ");
for(i=0;(ch=getchar()) != 'n' && i<30; i++)
counts[toupper(ch)-'A']++;
}
bool equal_array(int counts1[26],int counts2[26])
{
int i;
bool is_anagram=false;
for(i=0; i<30; i++)
{
counts1[i]= counts1[i] - counts2[i];
if(counts1[i] == 0)
{
is_anagram=true;
}
else
{
is_anagram=false;
break;
}
}
retu is_anagram;
}
int main()
{
int first_word[26]={0};
int second_word[26]={0};
read_word(first_word);
read_word(second_word);
if( equal_array(first_word,second_word) == true)
printf("Anagram");
else
printf("Not Anagram");
retu 0;
}
I'd appreciate any help I could get.
