2015-12-12 26 views
0
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)

+0

使'isPalindrome'内部版本具有'length'作为参数,所以你不需要static int 。尽量避免使用静态。 – wimh

+1

'static int length = strlen(str);' - >'int length = strlen(str);'也'length - = 2;'然后复制并更新EOS(字符串结束) – BLUEPIXY

+0

我想在函数中使用1个参数(char * str) –

回答

1

具有静态存储持续时间的任何对象只能使用常量表达式进行初始化。 strlen(str)不是一个常量表达式。

§6.7.9,初始化

All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.

相反,你可以删除static预选赛,并通过简单地使用额外的变量重写逻辑:

int isPalindrome(char *str, size_t st, size_t end) 
{ 
    if (st >= end) return 1; 
    return (str[st] == str[end]) && isPalindrome(str, st+1, end-1); 
} 

,并呼吁:

if (isPalindrome(word, 0, strlen(word) - 1)) 

在你现有的实现,您需要更改NUL终止并删除static

int isPalindrome(char *str) 
{ 
    int length = strlen(str); 
    if (length < 1) 
     return 1; 
    if (str[0] == str[length - 1]) 
    { 
     str[length - 1] = '\0'; 
     length -= 2; 
     return isPalindrome(str + 1); 
    } 
    else return 0; 
} 

使word副本,并把它传递:

char temp[25]; 
    strcpy(temp, word); 
    if (isPalindrome(temp)) { 
+0

谢谢你的回答。 但我想在函数中有1个参数(输入字符串)。 int isPalindrome(char * str) –

+0

@ArayaSiriadun你可以使用循环而不是递归吗? –

+0

没有。我无法使用。 T^T –

1

static是像一个全局变量。您不能将动态值分配给全局变量。您只能将常量值分配给全局变量。

那么试试这个,

static int length; 
length = strlen(str); 

然后在你的情况,你不需要做该变量作为一个static

被修改代码

int isPalindrome(char *str) 
{ 
    int length = strlen(str); 
    while(1){ 
      if (length < 1) 
        return 1; 
      if (str[0] == str[length - 1]) 
      { 
       length -= 2; 
       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; 
}            
+0

我尝试这是工作。 但输出不正确。我该怎么办? –

+1

此代码是工作。但是 对不起,我忘了告诉你。 我不允许编辑主函数 –

+0

@ArayaSiriadun试试这个。 –

0
int isPalindrome(char *str) 
{ 
    int length = strlen(str); 
    if (length <= 1) 
     return 1; 
    if (str[0] == str[length - 1]) 
    { 
     str[length - 1] = '\0'; 
     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\n", word); 
    else 
     printf("%s isn't Palindrome\n", word); 
    system("pause"); 
    return 0; 
} 

此代码是好的。 但它有一点点的错误。

Please enter a word: abcba 
abc is Palindrome 

答案是正确的。 但字符串不正确 (我不能编辑main,我只能编辑其他函数)

+0

您可以在函数本身中创建副本:http://ideone.com/SQRWZD –