程序提示用户键入存储在两个不同数组中的两个单词。 如果单词是anagram,它会打印“Anagram”,如果不是则打印“Not Anagram”。我为所有的字母组成了一个数组,字母'a'被存储为{1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...},填满了整个字母组合。C程序来查找两个单词是否是anagrams(我的代码在里面)
然后我比较了这两个数组,以确定它们是否是我将每个字母减去的相同单词,如果它们是0(相互抵消),则它们是Anagrams。 这是我的代码到目前为止,我不知道我做错了什么。我很肯定布尔函数有什么问题。
#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;
}
}
return 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");
return 0;
}
我很感激任何帮助,我可以得到。