简体   繁体   English

C ++回文不起作用

[英]C++ palindrome not working

I wanted to create function that returns true/false according if the input is a palindrome or not, when given abcddcba or aba it does not give true, but it should . 我想创建一个根据输入是否为回文而返回true / false的函数,当给出abcddcba或aba时,它不会给出true,但是应该。 plz help 请帮助

bool checkPalindrome(char input[],int p=0) {
    if(input[1]=='\0'){
        return true;
    }
    if(sizeof(input)%2==0) { 
        int a = sizeof(input); 
        for(int i=0;i<(a/2);i++) {  
            if(input[0+i]==input[a-i-2]){
                p++;
            }
        }
        if(p==a/2){
            return true;
        } else{
            return false;
        }
    }
    else{
        int a = sizeof(input); 
        for(int i=0;i<((a-1)/2);i++)
        {
            if(input[0+i]==input[a-i-2]){
                p++;
            }
        }
        if(p==(a-1)/2){
            return true;
        } else{
            return false;
        }
    }
}

C++ style: C ++样式:

bool checkPalindrome(const std::string& str)
{
    size_t len = str.size();
    for (size_t i = 0; i < len/2; i++)
    {
        if (str[i] != str[len-1-i])
            return false;
    }
    return true;
}

C style: C风格:

bool checkPalindrome(const char* str)
{
    size_t len = str ? strlen(str) : 0;
    for (size_t i = 0; i < len/2; i++)
    {
        if (str[i] != str[len-1-i])
            return false;
    }
    return true;
}

In either case, you may need to evaluate (ask) if an empty string should be considered a palindrome or not. 无论哪种情况,您都可能需要评估(询问)是否应将空字符串视为回文。

your code is too long. 您的代码太长。 There's a simple way: iterating once over the elements of the string comparing the n-1 with 0 and n-2 with 1 and so on. 有一个简单的方法:在比较n-1与0以及n-2与1比较的字符串元素上迭代一次,依此类推。

If two characters are not identical return false otherwise continue checking: 如果两个字符不相同,则返回false,否则继续检查:

bool checkPalindrome(char* str) {
    const int size = strlen(str);
    for(int i(0), j(size) - 1); i < size / 2; i++, j--)
        if(str[i] != str[j])
            return false;
    return true;
}

int main(){

    char* str = "level";
    cout << checkPalindrome(str);

    cout << endl << endl;
    return 0;
}

C++ version based on strings and iterators (only for the sake of completeness, the other answers are already very nice) : 基于字符串和迭代器的C ++版本(仅出于完整性考虑,其他答案已经非常好了):

bool checkPalindrome(const string& str) {
    for (auto p = str.begin(),q = str.end();  p!=q && p!=q+1; p++ ) 
        if (*p!=*--q)       // if char from front doesn't match char from rear ?
            return false;   // then it's not a palindrome !
    return true;
}

Explanations: the iterator p starts at the front of the string and q at the rear (after the last char). 说明:迭代器p从字符串的开头开始,而q在字符串的后面(最后一个字符之后)。 p will advance and q will go backwards. p将前进而q将后退。 If p reaches q or if p has passed q (special case if the word has an even length), it's over and we can conclude a palindrome. 如果p达到qp已超过q (特殊情况下单词长度为偶数),则结束,我们可以得出回文式。 But if before, there is any difference between the char at p and the char preceeding q , it's not a palindrome. 但是如果以前,在p处的字符和在q之前的字符之间有任何区别,则不是回文。

Online demo, with little test suite 在线演示,几乎没有测试套件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM