我的程序检查两个单词是否是字谜。我在将其值设置为 0 时遇到问题。我已经设法通过使用for
循环将所有值设置为 0 来解决问题,但我想知道是否可以int counts[26] = {0};
改用,显然不能不修改,因为编译器显示错误:
8 C:\Dev-Cpp\Templates\anagrams_functions.c 'counts' 重新声明为不同类型的符号
这是代码:
#include <stdio.h>
#include <ctype.h>
void read_word(int counts[26])
{
int i;
char ch, let[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W', 'X', 'Y', 'Z'};
int counts[26] = {0};
while((ch = toupper(getchar())) != '\n'){
for(i = 0; i < 26; i++){
if (let[i] == ch)
counts[i]++;
}
}
}
int equal_array(int counts1[26], int counts2[26])
{
int i;
for(i = 0; i < 26; i++){
if(counts1[i] != counts2[i])
return 0;
else continue;
}
return 1;
}
int main(void)
{
int counts1[26] = {0};
int counts2[26] = {0};
printf("Enter first word: ");
read_word(counts1);
printf("Enter second word: ");
read_word(counts2);
if(equal_array(counts1, counts2))
printf("The words are anagrams");
else
printf("The words are not anagrams");
getch();
return 0;
}