C++ 字符串检查总是返回假回文

C++ 字符串检查总是返回假回文,c++,c++11,C++,C++11,我正在编写一个程序来检查用户输入的字符串是否是回文 我的程序总是返回false,即使字符串是已知的回文。 我认为我的问题在于回文检查器函数,因为它总是将主函数中的答案变量设置为false。然而,我不确定为什么总是这样 #include "stdafx.h" #include <iostream> #include <String> using namespace std; char ReadPalIn(string Pal, int len)//converts th

我正在编写一个程序来检查用户输入的字符串是否是回文 我的程序总是返回false,即使字符串是已知的回文。 我认为我的问题在于回文检查器函数,因为它总是将主函数中的答案变量设置为false。然而,我不确定为什么总是这样

#include "stdafx.h"
#include <iostream>
#include <String>

using namespace std;

char ReadPalIn(string Pal, int len)//converts the string into a char array
{
    char PalIn[100];
    if (len > 100)
    {
        cout << "ERROR: Palindrome possibliity too big" << endl;
        system("Pause");
    }
    else
    {
        for (int i = 0; i < len; i++)
        {
            PalIn[i] = Pal[i];
        }
    }
    return *PalIn;
}

bool PalindromeChecker(char Pal[],int start, int end)//checks recursively if a string is a palidrome
{
    if (start == end)
    {
        return true; //since there is only one character
    }
    else if (Pal[start] != Pal[end])
    {
        //cout << "hi" << endl;
        return false;//since that will be the case that decides when something stops being a palidrome
    }
    else if (start < end + 1)
    {
        //cout << "hi" << endl;
        return PalindromeChecker(Pal, start++, end--);//since we checked both the first and last characters of the char array for palandrominess. <- word of the year?
    }
    else
    {
        return true;//base case is the string is a palindrome
    }
}

int main()//test if a word is a palidrome using the functions
{
    int lengthOfPal = 0;
    string PalInd = "abba";
    bool Answer = true;
    cout << "Hello what string would you like to check?" << endl;
    getline(cin, PalInd);
    lengthOfPal = PalInd.length();
    cout << "You input is: " << PalInd << endl;
    cout << "Its Length is: " << lengthOfPal << endl;
    system("Pause");
    char PalIn[100] = { ReadPalIn(PalInd, lengthOfPal) };

    Answer = PalindromeChecker(PalIn, 0, lengthOfPal);
    if (Answer == true)
    {
        cout << PalInd << ": is a palidrome" << endl;
        system("Pause");
    }
    else if(Answer == false)
    {
        //cout << "hi" << endl;
        cout << PalInd << ": is not a palidrome" << endl;
        system("Pause");
    }
    return 0;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
char ReadPalIn(string Pal,int len)//将字符串转换为char数组
{
查尔·佩林[100];
如果(len>100)
{

cout您总是返回
false
,因为您没有比较您认为正在比较的内容

在对
PalindromeChecker()
的初始调用中,您使用
lengthOfPal
作为
end
参数。由于字符串的索引为零,因此初始调用应使用
lengthOfPal-1

假设字符串是
abba
a
字符位于
[0]
[s.length()-1]
(索引
[3]


另一个问题是将
start++
end--
作为递归调用中的参数传递。这些是后增量和后减量运算符,这意味着它将把
start
end
的当前值传递到递归调用中,然后调整它们的值。因为如果已将函数设置为递归,则每次对
start
end
都将使用相同的值。请使用预增量
++start
和预减量
--end
来避免此问题。

堆栈溢出不是免费的调试服务,您应该在使用调试器或其他更简单的方法,如调试打印语句。您还可以分别测试代码的每个部分,以确定代码的哪一部分导致问题,并做出正确的判断。这不是您代码中出现错误的唯一一次,学习调试程序将比让某人来调试程序更有帮助为您找出错误。您为什么要用字符数组折磨自己?您知道如何使用
std::string
,它为您管理内存内容。您的问题在于
ReadPalIn
函数。它返回单个字符(字符串中的第一个字符)。然后尝试将字符分配给字符数组。如果可以,请始终使用
std::string
,这样就不必处理类似的内容。可以使用
PalInd.c_str()
读取
std::string
的基本字符数组。无法从函数返回指向局部变量的指针。