我想創建一個程序來檢查一個給定的數組/字符串是否是迴文並且它不工作。該程序只在每個給定的陣列上打印「0」,即使在迴文中也是如此。程序檢查一個數組是否是迴文
int main()
{
char string[100]= {0};
char stringReverse[100]= {0};
int temp = 0;
int firstLetter = 0;
int lastLetter = 0;
printf("Please enter a word or a sentence: ");
fgets(string, 100, stdin);
strcpy(stringReverse , string); // This function copies the scanned array to a new array called "stringReverse"
firstLetter = 0;
lastLetter = strlen(string) - 1; //because in array, the last cell is NULL
// This while reverses the array and insert it to a new array called "stringReverse"
while(firstLetter < lastLetter)
{
temp = stringReverse[firstLetter];
stringReverse[firstLetter] = stringReverse[lastLetter];
stringReverse[lastLetter] = temp;
firstLetter++;
lastLetter--;
}
printf("%s %s", stringReverse, string);
if (strcmp(stringReverse , string) == 0)
{
printf("1");
}
else
{
printf("0");
}
}
對於這樣一個簡單的任務代碼太多。 –
我已經發布了類似問題的答案[這裏](http://stackoverflow.com/a/33806921/4487286),你可以很容易地適應你的需求 – milevyo