简体   繁体   English

C++ 回文函数

[英]C++ palindrome function

 #include <iostream>
 #include <string>


using namespace std;


bool isPalindrome(string str){



    for(int i = 0; i <= str.length()-1; i++){
        if(str[i] != str[str.length()-1-i]){
            return false;
        }else {
            return true;
        }
    }
}

main(){

    string text;

    do{
        cout << "Enter some Text: " << endl;
        cin >> text;
        if(isPalindrome(text)){
            cout << "The text is a palindrome" << endl;
        }
        else{
            cout << "The text is not a palindrome" << endl;
        }

    }while(text != "Q");

    return 0;
}

Can someone explain to me what is wrong with my code?有人可以向我解释我的代码有什么问题吗? If I input "otto" as text, the code produces the correct answer.如果我输入“otto”作为文本,代码会产生正确的答案。 If In input "ottop" as text, the code is working as well, but if I input "ottopo" i get as output that "ottopo" is a palindrome, which it obviously is not.如果输入“ottopo”作为文本,代码也能正常工作,但如果我输入“ottopo”,我得到的输出是“ottopo”是回文,显然不是。

I seem to be missing something or have maybe overlooked something.我似乎遗漏了一些东西,或者可能忽略了一些东西。 I know i could use c++ standard library funtions for this, but i really want to know why it's not working the way i want it to.我知道我可以为此使用 C++ 标准库函数,但我真的很想知道为什么它不能按我希望的方式工作。

I have implemented palindrome functions in java, python, javascript, ruby ect...I just can't find my mistake within this c++ code!我已经在 java、python、javascript、ruby 等中实现了回文函数……我只是在这个 C++ 代码中找不到我的错误! I know it's really simpel, but it's sooo frustrating!我知道这真的很简单,但是太令人沮丧了!

You are returning true on the first iteration of the loop.您在循环的第一次迭代中返回 true。 And you only need to iterate on half the string.而且你只需要迭代一半的字符串。

Corrected:更正:

    size_t len = str.length();

    for(int i = 0; i < len/2; i++){
        if(str[i] != str[len-1-i]){
            return false;
        }
    }
    return true;

You don't have to have a return true after the else statement there.您不必在那里的 else 语句之后return true Put that outside the for loop and your code should work.把它放在 for 循环之外,你的代码应该可以工作。

As an extra nitpick, you don't have to check until strlen(s) - 1, but strlen(s) / 2 is actually enough.作为一个额外的挑剔,你不必检查直到 strlen(s) - 1,但 strlen(s) / 2 实际上就足够了。 I'll leave figuring that out to you, as it's quite straightforward and not too different from what you are doing, at all.我会留给你解决这个问题,因为它非常简单,并且与你正在做的事情并没有太大的不同。

As it has been said, the return true;如前所述, return true; should not be in the else , but after the end of the for loop.不应在else ,而应在for循环结束之后。 This can also be accomplished in just a few lines.这也可以通过几行来完成。 A string is a palindrome if it is the same reversed as it is normally.如果字符串与正常情况相同,则它是回文。 All you need is所有你需要的是

reversedText = reverse(text.begin(), text.end());
if (text == reversedText)
    //indicate that it is a palindrome
else
    //indicate that it is not a palindrome`

Just be sure to include algorithm.一定要包括算法。 I can understand if you don't want to use a library for some reason, but I just want to make sure that you know that this can be accomplished very easily.如果您出于某种原因不想使用库,我可以理解,但我只想确保您知道这可以很容易地完成。

You should set a flag to 1 if it is not a palindrome, then break after:如果它不是回文,您应该将标志设置为1 ,然后在之后中断:

for (int i = 0; i < len/2; i++) {
    if (str[i] != str[len-1-i]) {
        // here .......
    }

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

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