두 개의 다른 배열에 저장된 두 단어를 입력하라는 메시지가 표시됩니다. 단어가 아나그램이라면 "아나그램"을, 그렇지 않으면 "아나그램이 아님"을 인쇄합니다. 모든 알파벳 글자에 대한 배열을 만들었습니다. 글자 'a'는 {1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...} 글자의 전체 배열을 채 웁니다.C 두 단어가 아나그램 (내부 코드)인지 확인하는 프로그램
다음 두 배열을 비교하여 동일한 단어인지 확인한 다음 각 문자를 빼고 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;
}
내가 얻을 수있는 도움에 감사드립니다.