int isPalindrome(char *str)
{
static int length = strlen(str);
if (length < 1)
return 1;
if (str[0] == str[length - 1])
{
length -= 2;
return isPalindrome(str + 1);
}
else return 0;
}
int main() {
char word[25];
printf("Please enter a word: ");
scanf("%s", word);
if (isPalindrome(word))
printf("%s is Palindrome", word);
else
printf("%s isn't Palindrome", word);
return 0;
}
我运行此代码后。它有错误ERROR: Initializer is not a constant
。 在这一行。 我该怎么办?错误:初始化程序不是一个常量
static int length = strlen(str);
函数必须有1个参数(输入字符串)==> isPalindrome(字符* STR)
使'isPalindrome'内部版本具有'length'作为参数,所以你不需要static int 。尽量避免使用静态。 – wimh
'static int length = strlen(str);' - >'int length = strlen(str);'也'length - = 2;'然后复制并更新EOS(字符串结束) – BLUEPIXY
我想在函数中使用1个参数(char * str) –