我正在探索處理字符串和我有這個特定程序的一些問題。有人可以幫助解釋爲什麼以及如何運作x < i/2
以及word[i - 1 - x]
。迴文C程序混亂
爲什麼一定要i/2
?
爲什麼它必須是word[i - 1 - x]
?
#include <stdio.h>
int main()
{
char word[15];
int i, x;
printf("Enter a word\n");
scanf("%s", word);
for(i = 0; word[i] != '\0';)
i = i + 1;
for(x = 0; x< i/2; x++)
if(word[x] != word[i-1-x])
{printf("Your word is not a palindrome\n");}
else
{
printf("Your word is a palindrome\n");
}
}