迹忆客 专注技术分享

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

在 C++ 中用递归函数检查回文字符串

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

本文将介绍几种在 C++ 中使用递归函数检查回文字符串的方法。


使用递归函数检查回文字符串

递归函数是从函数主体中调用自身的函数。递归方法不是实现回文校验算法的最佳方法。isPalindrome 函数接受三个参数,并返回一个布尔值,以指示所传递的 string 是否是回文。第二个和第三个参数用于存储字符串序列的开始和结束位置。作为第一个条件,我们评估字符串长度是否为 1,如果是,则函数立即返回一个肯定值。接下来,比较第一个和最后一个字符,如果不相等,则该函数返回 false。否则,执行将继续检查字符串是否在当前 sten 位置的中间包含更多字符。如果条件评估为真,则使用移位的位置和相同的字符串值进行递归调用。另一方面,错误条件会强制函数返回 true

#include <iostream>
#include <string>

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

bool isPalindrome(const string &s, size_t st, size_t en)
{
    if (s.length() == 1)
        return true;

    if (s[st] != s[en-1])
        return false;

    if (st < en + 1)
        return isPalindrome(s, st + 1, en - 1);

    return true;
}

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

    isPalindrome(s1, 0, s1.length()) ?
    cout << "s1 is a palindrome" << endl :
    cout << "s1 is not a palindrome" << endl;

    isPalindrome(s2, 0, s2.length()) ?
    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

请注意,先前的解决方案无法识别包含大小写字母和空格(例如 s2 对象)的回文。因此,我们需要将字符串转换为相同的大小写字母,并删除所有空格以正确找到所有有效回文。std::trasform 算法和 erase-remove 惯用法用于将字符串解析为 isPalindrome 函数的兼容格式。

#include <iostream>
#include <string>

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

bool isPalindrome(const string &s, size_t st, size_t en)
{
    if (s.length() == 1)
        return true;

    if (s[st] != s[en-1])
        return false;

    if (st < en + 1)
        return isPalindrome(s, st + 1, en - 1);

    return true;
}

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

    transform(s2.begin(), s2.end(), s2.begin(),
              [](unsigned char c){ return tolower(c); } );
    s2.erase(remove(s2.begin(), s2.end(), ' '), s2.end());


    isPalindrome(s2, 0, s2.length()) ?
    cout << "s2 is a palindrome" << endl :
    cout << "s2 is not a palindrome" << endl;

    return EXIT_SUCCESS;
}

输出:

s2 is a palindrome

使用 std::equal 算法检查回文字符串

std::equal 是 C++ 标准模板库的一部分,可用于比较范围。因此,由于 string 类具有迭代器接口,我们可以使用 beginrbegin 函数指定其范围。不过请注意,以下 std::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(s2) ?
    cout << "s2 is a palindrome" << endl :
    cout << "s2 is not a palindrome" << endl;

    return EXIT_SUCCESS;
}

输出:

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便