C++ 检查字符串是否为回文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8362572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:26:19  来源:igfitidea点击:

Check if a string is palindrome

c++

提问by Wil Prim

Possible Duplicate:
to find if a given string is palindrome or is not palindrome

可能的重复:
查找给定的字符串是回文还是不是回文

I need to create a program that allows a user to input a string and my program will check to see if that string they entered is a palindrome (word that can be read the same backwards as it can forwards).

我需要创建一个允许用户输入字符串的程序,我的程序将检查他们输入的字符串是否是回文(可以向后读取和向前读取相同的单词)。

回答by Cubbi

Note that reversing the whole string (either with the rbegin()/rend()range constructor or with std::reverse) and comparing it with the input would perform unnecessary work.

请注意,反转整个字符串(使用rbegin()/rend()范围构造函数或使用std::reverse)并将其与输入进行比较将执行不必要的工作。

It's sufficient to compare the first half of the string with the latter half, in reverse:

将字符串的前半部分与后半部分进行反向比较就足够了:

#include <string>
#include <algorithm>
#include <iostream>
int main()
{
    std::string s;
    std::cin >> s;
    if( equal(s.begin(), s.begin() + s.size()/2, s.rbegin()) )
        std::cout << "is a palindrome.\n";
    else
        std::cout << "is NOT a palindrome.\n";
}

demo: http://ideone.com/mq8qK

演示:http: //ideone.com/mq8qK

回答by Seth Carnegie

Just compare the string with itself reversed:

只需将字符串与自身反转进行比较:

string input;

cout << "Please enter a string: ";
cin >> input;

if (input == string(input.rbegin(), input.rend())) {
    cout << input << " is a palindrome";
}

This constructor of stringtakes a beginning and ending iterator and creates the string from the characters between those two iterators. Since rbegin()is the end of the string and incrementing it goes backwards through the string, the string we create will have the characters of inputadded to it in reverse, reversing the string.

这个构造函数string接受一个开始和结束迭代器,并从这两个迭代器之间的字符创建字符串。由于rbegin()是字符串的结尾并且递增它会在字符串中向后移动,因此我们创建的字符串将input反向添加字符,从而反转字符串。

Then you just compare it to inputand if they are equal, it is a palindrome.

然后你只需将它input与它进行比较,如果它们相等,它就是一个回文。

This does not take into account capitalisation or spaces, so you'll have to improve on it yourself.

这不考虑大小写或空格,因此您必须自己改进。

回答by selbie

bool IsPalindrome(const char* psz)
{
    int i = 0;
    int j;

    if ((psz == NULL) || (psz[0] == '
bool IsPalindrome(const string& str)
{
    if (str.empty())
        return false;

    int i = 0;                // first characters
    int j = str.length() - 1; // last character

    while (i < j)
    {
        if (str[i] != str[j])
        {
            return false;
        }
        i++;
        j--;
    }
    return true;
}
')) { return false; } j = strlen(psz) - 1; while (i < j) { if (psz[i] != psz[j]) { return false; } i++; j--; } return true; }

// STL string version:

// STL 字符串版本:

public static string Reverse(string s) {
    if (s == null || s.Length < 2) {
        return s;
    }

    int length = s.Length;
    int loop = (length >> 1) + 1;
    int j;
    char[] chars = new char[length];
    for (int i = 0; i < loop; i++) {
        j = length - i - 1;
        chars[i] = s[j];
        chars[j] = s[i];
    }
    return new string(chars);
}

回答by Madhu Kasic

Reverse the string and check if original string and reverse are same or not

反转字符串并检查原始字符串和反向字符串是否相同

回答by varatis

I'm no c++ guy, but you should be able to get the gist from this.

我不是 C++ 人,但你应该能够从中得到要点。

##代码##