C++ 判断回文字符串【两种方法】

cpp:判断回文字符串的两种简单方法

#include <iostream>
#include <string>
using namespace std;

bool testPalindrome(const string s){
    int i = 0;
    int j = s.size() - 1;
    for(; i < j; i++, j--){
        if(s[i] != s[j])
            break;
    }
    if(i >= j)
        return 1;
    else
        return 0;
}
bool testPalindrome2(const string s){
    int i = 0;
    for(; i < s.size()/2; i++){
        if(s[i] != s[s.size() - 1 -i]){
            return 0;
        }
    }
    return 1;
}
int main()
{
    string a;
    cin>>a;
    if (testPalindrome(a) && testPalindrome2(a))
        cout << "Palindrome string" << endl;
    else
    {
        cout << "Non-palindrome string" << endl;
    }
}


版权声明:本文为qq_33177268原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。