ユーザーからの文入力を取得し、単語を逆順に出力し、アナグラムをチェックし、回文をチェックする必要があるという課題に取り組んでいます。アナグラムで機能する関数があり、回文関数がほとんど機能しています。今のところ、関数を機能させるために 2 つの単語だけを求めています。しかし、どういうわけか、私が要求する両方の単語に対して長い回文 (例: レースカーまたは母または父と比較して不機嫌) を入力するたびに、回文機能が台無しになります。
コードは次のとおりです。
#include <stdio.h>
#include <ctype.h> //Included ctype for tolower / toupper functions
#define bool int
#define true 1
#define false 0
//Write boolean function that will check if a word is a palindrome
bool palindrome(char a[])
{
int c=0;
char d[80];
//Convert array into all lower case letters
while (a[c])
{
a[c] = (tolower(a[c]));
c++;
}
c = 0;
//Read array from end to beginning, store it into another array
while (a[c])
c++;
while(a[c] != 0 && c > -1)
{
d[c] = a[c];
c--;
}
c = 0;
while(a[c])
{
printf("%c", d[c]);
printf("%c", a[c]);
c++;
}
//If two arrays are equal, then they are palindromes
for(c = 0; a[c] && d[c]; c++)
{
while(a[c] && d[c])
{
if(a[c] != d[c])
return false;
}
}
return true;
}
int main(void)
{
char a[80], b[80];
bool flagp;
//Prompt user to enter sentence
printf("Enter a word: ");
gets(a);
flagp = palindrome(a);
if (flagp)
{
printf("\nThe word is a palindrome.");
}
else
{
printf("\nThe word is not a palindrome.");
}
return 0;
}
これを出力します。
Enter first word: racecar
_r▬a↨c e c a r
The word is not a palindrome.
ただし、「racecar」と入力すると、回文ではないと誤って示されます。
私が間違っていることを教えてください:'(