Kodewick
开始学 →
基础←→0 / 50

判断回文字符串

#045基础s[i] != s[j]约 5 分钟

回文字符串正着读和倒着读一模一样,比如 "level"、"abba"。写 is_palindrome(s):是回文返回 true,不是返回 false。大小写算不同的字符("Aba" 不是回文);空字符串和只有一个字符的都算回文。

示例
is_palindrome("level")→True
is_palindrome("ab")→False
is_palindrome("")→True
函数bool is_palindrome(const char *s)
main.c
#include <stdbool.h>
#include <string.h>

bool is_palindrome(const char *s) {
    int i = 0;
    int j = (int)strlen(s) - 1;
    while (i < j) {
        // 在这里写:两头的字符不一样,就不是回文
        i++;
        j--;
    }
    return true;
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。