制作回文程序c ++有困难

时间:2015-03-17 08:18:12

标签: c++ algorithm palindrome

嗨,这是我的回文计划代码:

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();
    }
}

当我的输入是赛车时,它会读取并说它是一个回文,但是当我的输入是赛车(带有空格)时它不会读取并且它表示它不是回文。 我的意图是忽略所有的空间。任何帮助都感激不尽! 提前谢谢!

** editted 所以我换了我的cin&gt;&gt;输入到getline(cin,输入)并且它不允许我输入我的单词或短语

3 个答案:

答案 0 :(得分:4)

如果您在阅读输入后首先删除所有空格,可能会有效吗?

#include <algorithm>

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

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

答案 1 :(得分:1)

问题

回文是一个拼写向后和向后拼写的单词。因此,你可以肯定,从外面开始,字母必须是相同的,直到你检查相同的字母(字母总数是奇数)或字母搜索的东西/审查员/标记(让我们称它们为迭代器)十字交叉。

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

你如何做(实施)

让我们假设我们有两个变量作为迭代器,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;
}

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

答案 2 :(得分:0)

看看这是否有效。

    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;
    }