我想出了如何解决字符串问题,但似乎无法使它起作用。
也许是因为我正在使用scanf。
请指教 :)

#include<stdio.h>
#include<string.h>

int do_palindrome(char *str, int offset){
int ok = 1;
int length = strlen(str);

if(length/2 > 0)
    ok = (str[0] == str[length - 1 - offset])?
            do_palindrome(++str, ++offset):0;

return ok;
}
int main(){
int i = 0;
int ok = 0;
char* str[1] ;

    scanf("%c", str[1]);
    ok = do_palindrome(str[0], 0);
    printf("%s is palindrome? : %d\n", str[0], ok);


printf("Finished!");
return 0;

}

最佳答案

char* str[1] ;


声明一个char指针的数组

scanf("%c", str[1]);


读取单个字符,但尝试将其放置在数组的末尾(C数组从零开始)。

我认为您想读取一个字符串(char数组)。您可以使用

char str[20]; /* change the array size as required */
scanf("%19s", str); /* read one fewer chars than your array size */

10-07 19:18
查看更多