简体   繁体   English

C ++中的回文扫描仪

[英]Palindrome scanner in C++

I've got a little problem with my palindrome "Scanner" in C++. 我的C ++回文式“扫描仪”有一个小问题。

    #include <iostream>
    #include <string.h>
    #include <string>


    using namespace std;

    void start() {
        string eingabe;

        string umkehrung;
    cout << "Bitte geben Sie das Wort ein, von welchem getestet werden soll, ob es ein Palindrom ist!" << endl;
    cin >> eingabe;
    eingabe = ' ' + eingabe;
    for (int i = eingabe.length(); i >= 0; i--) {
        umkehrung = umkehrung + eingabe[i];

    }
    if (umkehrung == eingabe) {
        cout << "Das Wort '" + eingabe + "' ist ein Palindrom!" << endl;

    }
    else {
        cout << "Das Wort '" + eingabe + "' ist kein Palindrom!" << endl;
        cout << eingabe << endl;
        cout << umkehrung << endl;
    }


}

int main() {
    start();
    return 0;
}

It reverses the string input(Eingabe--->Umkehrung) and then checks if they are the same. 它反转字符串输入(Eingabe ---> Umkehrung),然后检查它们是否相同。 But somehow it does always say they are not the same, even if they look the same (I output them here: 但是无论如何,它总是说它们不一样,即使它们看起来一样(我在这里输出它们:

cout << eingabe << endl;
cout << umkehrung << endl;

You can use the algorithm provided by standard library to reverse the array. 您可以使用标准库提供的算法来反转数组。 This would look like this: 看起来像这样:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void start() {
    string eingabe;
    string umkehrung;

    cout << "Bitte geben Sie das Wort ein, von welchem getestet werden soll, ob es ein Palindrom ist!" << endl;
    cin >> eingabe;

    umkehrung = eingabe;
    std::reverse(umkehrung.begin(), umkehrung.end());

    if (umkehrung == eingabe) {
        cout << "Das Wort '" + eingabe + "' ist ein Palindrom!" << endl;

    }
    else {
        cout << "Das Wort '" + eingabe + "' ist kein Palindrom!" << endl;
        cout << eingabe << endl;
        cout << umkehrung << endl;
    }
}

int main() {
    start();
    return 0;
}

You don't even need to bother creating the reverse string; 您甚至不需要费心创建反向字符串。 just use the reverse iterator and std::equal : 只需使用反向迭代器和std::equal

#include <string>
#include <iostream>
#include <algorithm>

void check (const std::string& s) {
    auto s_is_a_palindrome = std::equal(s.begin(), s.end(), s.rbegin());
    std::cout << '"' << s << "\" is " << (s_is_a_palindrome ? "" : "not ")
              << "a palindrome\n";
}

int main() {
    check("hello olleh");
    check("hello, world");
}

And this program will indeed produce : 这个程序确实会产生

"hello olleh" is a palindrome
"hello, world" is not a palindrome

Note: You can stop comparing at half-length, ie s.begin() + s.length() / 2 should also be fine instead of s.end() . 注意:您可以停止以半角比较,即s.begin() + s.length() / 2应该也可以代替s.end() Comparing the other half is just doing the same comparisons in reverse. 比较另一半只是反向进行相同的比较。

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

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