难以制作回文程序c ++

having a difficulty with making a palindrome program c++

嗨,这是我的回文程序代码:

void palindrome()
{
    string input;
    bool checkInput, palindrome;
    palindrome = true;
    do
    {
        checkInput = false;
        cout << "Enter a word, phrase or sentence :\n";
        getline(cin, input);

        for (unsigned int i = 0; i < input.size(); i++)
        {
            if (input[i] < 65 || input[i] > 90 && input[i] < 97 || input[i] > 122)
            {
                checkInput = true;
            }
        }



    } while (checkInput);

    for (unsigned int i = 0, j = input.size() - 1; i < input.size(); i++, j--)
    {
        if (input[i] != input[j] && input[i] + 32 != input[j] && input[i] - 32 != input[j])
        {
            palindrome = false;
            break;
        }
    }

    if (palindrome)
    {
        cout << "\n\nTo consider only letters and digits:\n";
        cout << input << "\nYes, it is palindrome!\n";
        cout << "\t\t Press <Enter> key back to menu";
        fflush(stdin);
        cin.get();
    }
    else
    {
        cout << "\n\nTo consider only letters and digits:\n";
        cout << input << "\nNOPE, it's not palindrome\n";
        cout << "\t\t Press <Enter> key back to menu";
        fflush(stdin);
        cin.get();
    }
}

当我的输入是 racecar 时,它读取并说它是回文,但是当我的输入是 race car(带有 space)时,它不会读取并且它说它不是回文。 我的意图是忽略所有 space。任何帮助都感激不尽! 提前致谢!

**已编辑 所以我将我的 cin >> 输入切换到 getline(cin, input) 并且它不让我输入我的单词或短语

如果您在读取输入后先删除所有空格,也许会奏效?

#include <algorithm>

str.erase(remove_if(str.begin(), str.end(), isspace), str.end());

您正在检查的 ASCII 值中没有空格,因此 while 循环在第一个空格处结束。

看看这是否有效。

    for (unsigned int i = 0, j == input.size() - 1; i < input.size();)
    {
        //Ignore spaces
        if (input[i] == ' ')
        {
           ++i; continue;
        }
        if (input[j] == ' ')
        {
           --j;continue;
        }

        //Automatic palindrome analysis 
        if (input[i] != input[j])
        {
            palindrome = false;
            break;
        }
        ++i;--j;
    }

问题

回文是一个单词,前后拼写相同。因此,您可以确定,从外到内,字母必须相同,直到您检查相同的字母(字母总数为奇数)或字母搜索 things/examiners/markers(让我们称它们为 迭代器) 纵横交错。

如何从外到内检查一对字母? 通过使用从第一个到最后一个位置的索引循环与从后到前的索引循环串联。

你是怎么做的(实施)

假设我们有两个变量作为迭代器,i 和 j。我将向前移动,而 j 将向后移动。它们将从两端开始:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    //This is where our word will be.
    std::string str;
    
    //Get input
    std::cout << "Input your word, please!" << std::endl;
    std::getline(std::cin, str);
    
    //Let's use std::erase to take away our whitespaces from the
    //C++11 library <algorithm>
    str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
    
    //Initialize i and j, our iterators
    //I use auto because the iterator type is long. It's a reason why auto was invented.
    auto i = str.begin();
    auto j = str.end() - 1;
    //You see, str.end() is actually the END, and not the last letter.
    //That's why it has a -1.
    
    bool is_palindrome = true;
    
    while (i < j) //While the two haven't crossed yet
    {
        //This std::cout shows you what i and j are at each repeat
        std::cout << "i = " << *i << " ||| j = " << *j << std::endl;
        
        //If the two characters marked by the two iterators are not equal
        if (*i != *j)
        {
            is_palindrome = false;
            break;
        }
        else
        {
            //Let's continue.
            ++i;
            --j;
        }
    }
    
    //If the loop gets to this point successfully, it's a palindrome.
    if (is_palindrome)
        std::cout << str << " is a palindrome!" << std::endl;
    else
        std::cout << str << " is not a palindrome." << std::endl;
        
    return 0;
}

希望这对您有所帮助。请记住为 C++11 功能使用 -std=c++11 进行编译!