检查字符串是否为回文

Check if a string is palindrome

本文关键字:回文 是否 字符串 检查      更新时间:2023-10-16

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

注意,反转整个字符串(使用rbegin()/rend()范围构造函数或std::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";
}

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

将字符串与自身反向比较:

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进行比较,如果它们相等,则它是一个回文。

这没有考虑到大写或空格,所以你必须自己改进。

bool IsPalindrome(const char* psz)
{
    int i = 0;
    int j;
    if ((psz == NULL) || (psz[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;
}
// 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;
}
#include <iostream>
#include <string>
bool isPalindrome(const std::string& str){
    if(str.empty()) return true;
    std::string::const_iterator itFirst = str.begin();
    std::string::const_iterator itLast = str.end() - 1;
    while(itFirst < itLast) {
        if (*itFirst != *itLast)
            return false;
    
        ++itFirst;
        --itLast;
    }
    return true;
}
int main(){
    while(1){
        std::string input;
        std::cout << "Eneter a string ...n";
        std::cin >> input;
        if(isPalindrome(input)){
            std::cout << input <<  " is palindrome.n";
        } else {
            std::cout << input << " is not palindrome.n";
        }
    }
    return 0;
}

检查字符串从两端开始到中间相交。如果不一致,返回false。

#include <iostream>
bool palidromeCheck(std::string str) {
    for (int i = 0, j = str.length()-1; i <= j; i++, j--)
        if (str[i] != str[j])
            return false;
    return true;
}
int main(){
    std::cout << palidromeCheck("mike");
    std::cout << palidromeCheck("racecar");
}

反转字符串并检查原字符串与反转字符串是否相同

我不是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);
}