迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中检查字符串是否为回文

作者:迹忆客 最近更新:2023/04/08 浏览次数:

本文将为大家讲解几种在 C++ 中如何检查一个字符串是否为回文的方法。


使用 string 复制构造函数与 rbegin/rend 方法来检查 C++ 中的字符串是否为回文

string 类对象支持使用运算符 == 进行比较,它可以用来寻找符合回文模式的字符串。由于回文意味着按相反的顺序匹配字符,我们需要用 rbeginrend 迭代器构造一个新的字符串对象。其余的则根据需要在 if 语句中构造。

在下面的例子中,我们声明了两个字符串 - 单字回文和多字回文。请注意,尽管带空格的字符串符合定义模式,但它不能将带有空格的字符串作为回文式检测。

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::equal; using std::remove;

int main(){
    string s1 = "radar";
    string s2 = "Was it a cat I saw";

    if (s1 == string(s1.rbegin(), s1.rend())) {
        cout << "s1 is a palindrome" << endl;
    } else {
        cout << "s1 is not a palindrome" << endl;
    }

    if (s2 == string(s2.rbegin(), s2.rend())) {
        cout << "s2 is a palindrome" << endl;
    } else {
        cout << "s2 is not a palindrome" << endl;
    }

    return EXIT_SUCCESS;
}

输出:

s1 is a palindrome
s2 is not a palindrome

使用 std::equal 方法在 C++ 中检查字符串回文

尽管最后一个实现在单字字符串上完成了工作,但它带来了创建一个对象副本和比较它们的完整范围的开销。我们可以利用 std::equal 算法来比较同一个 string 对象范围的前半部分和后半部分。std::equal 如果给定的两个范围中的元素相等,则返回布尔值 true。请注意,函数只需要一个迭代器-s1.rbegin() 用于第二个范围,因为范围的结束是以 first2 + (last1 - first1) 计算的。

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::equal; using std::remove;

int main(){
    string s1 = "radar";
    string s2 = "Was it a cat I saw";

    equal(s1.begin(), s1.begin() + s1.size()/2, s1.rbegin()) ?
        cout << "s1 is a palindrome" << endl :
        cout << "s1 is not a palindrome" << endl;

    equal(s2.begin(), s2.begin() + s2.size()/2, s2.rbegin()) ?
        cout << "s2 is a palindrome" << endl :
        cout << "s2 is not a palindrome" << endl;

    return EXIT_SUCCESS;
}

输出:

s1 is a palindrome
s2 is not a palindrome

使用自定义函数在 C++ 中检查字符串是否是回文

以前的方法对多字的字符串有不足之处,我们可以通过实现一个自定义函数来解决。本例演示了布尔函数 checkPalindrome,它接受 string&参数,并将其值存储在本地 string 变量中。然后用 transform 算法处理本地对象,将其转换为小写字母,并因此用 eras-remove 删除其中的所有空白字符。最后,我们在 if 语句条件中调用 equal 算法,并返回相应的布尔值。不过要注意的是,如果字符串是由多字节字符组成的,这个方法会失败。因此,应该实现支持所有通用字符编码方案的小写转换方法。

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::equal; using std::remove;

bool checkPalindrome(string& s) {
    string tmp = s;
    transform(tmp.begin(), tmp.end(), tmp.begin(),
              [](unsigned char c){ return tolower(c); } );
    tmp.erase(remove(tmp.begin(), tmp.end(), ' '), tmp.end());

    if (equal(tmp.begin(), tmp.begin() + tmp.size()/2, tmp.rbegin())) {
        return true;
    } else {
        return false;
    }
}

int main(){
    string s1 = "radar";
    string s2 = "Was it a cat I saw";

    checkPalindrome(s1) ?
        cout << "s1 is a palindrome" << endl :
        cout << "s1 is not a palindrome" << endl;

    checkPalindrome(s2) ?
        cout << "s2 is a palindrome" << endl :
        cout << "s2 is not a palindrome" << endl;

    return EXIT_SUCCESS;
}

输出:

s1 is a palindrome
s2 is a palindrome

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便