检查字符串是否是回文

时间:2011-12-02 20:59:04

标签: c++

  

可能重复:
  to find if a given string is palindrome or is not palindrome

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

7 个答案:

答案 0 :(得分:56)

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

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

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

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

答案 1 :(得分:34)

只需比较字符串与其自身的反转:

string input;

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

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

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

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

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

答案 2 :(得分:6)

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

    if ((psz == NULL) || (psz[0] == '\0'))
    {
        return false;
    }

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

}

// STL字符串版本:

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

// The below C++ function checks for a palindrome and 
// returns true if it is a palindrome and returns false otherwise

bool checkPalindrome ( string s )
{
    // This calculates the length of the string

    int n = s.length();

    // the for loop iterates until the first half of the string
    // and checks first element with the last element,
    // second element with second last element and so on.
    // if those two characters are not same, hence we return false because
    // this string is not a palindrome 

    for ( int i = 0; i <= n/2; i++ ) 
    {
        if ( s[i] != s[n-1-i] )
            return false;
    }
    
    // if the above for loop executes completely , 
    // this implies that the string is palindrome, 
    // hence we return true and exit

    return true;
}

答案 5 :(得分:0)

library(dplyr)
dat %>%
  mutate(across(where(is.numeric), ~ if_else(grepl("^L", A), -1, 1) * .))
#       A  B   C D
# 1     L -1 -11 A
# 2 Right  2  12 B
# 3     R  3  13 C
# 4  Left -4 -14 D

答案 6 :(得分:-9)

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

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);
}
相关问题