字符串是回文

问题描述 投票:0回答:1

我想知道为什么该函数没有返回。

我想通过仅检查字母来检查字符串是否是回文。

代码

#include <iostream>

using namespace std;

bool isPalindrome(string s)
{
    int strSize = s.size();
    if (strSize == 1 || strSize == 0)
    {
        cout << " i am gone bro" << endl;
        return true;
    }

    char first = (char)tolower(s[0]);
    char last = (char)tolower(s[strSize - 1]);

    if (!(first >= 'a' && first <= 'z'))
    {
        cout << "after trim first -> " << s.substr(1, strSize - 1) << endl;
        isPalindrome(s.substr(1, strSize - 1));
    }

    if (!(last >= 'a' && last <= 'z'))
    {
        cout << "after trim last -> " << s.substr(0, strSize - 2) << endl;
        isPalindrome(s.substr(0, strSize - 2));
    }

    if (first == last)
    {
        cout << "euqual " << first << endl
             << (s.substr(1, strSize - 2)) << endl;
        isPalindrome(s.substr(1, strSize - 2));
    }

    cout << " still running for " << s << " size " << strSize << endl;

    return false;
}

int main()
{
    bool ans = isPalindrome("A man, a plan, a canal: Panama");
    cout << "ans = " << ans;
    return 0;
}

我想通过仅检查字母来检查字符串是否是回文。

c++ algorithm
1个回答
0
投票

您正在将函数编写为递归函数(即调用自身的函数),但您并没有

return
'计算递归调用的值。

© www.soinside.com 2019 - 2024. All rights reserved.