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

正着倒着一样

#051基础常用算法字符串约 4 分钟

有的词倒过来读还是它自己,比如 level、noon。写 same_both_ways(word):模板已经复印了一份 back,用 reverse 把它倒过来,再和 word 比。大小写不同算不一样;空的也算一样。

示例
same_both_ways("level")→True
same_both_ways("hello")→False
函数bool same_both_ways(const string &word)
main.cpp
#include <algorithm>
#include <string>
using namespace std;

bool same_both_ways(const string &word) {
    string back = word;
    // 在这里写:把 back 倒过来
    return back == word;
}
Ctrl + Enter 也能运行
结果

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