【问题标题】:Need to check if the string is the same when read backwards [duplicate]向后读取时需要检查字符串是否相同[重复]
【发布时间】:2014-05-21 21:29:17
【问题描述】:

我不知道这里出了什么问题。不过我是一个全新的人。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <tchar.h>
#include <string>
using namespace std;


int main()
{
    int string_length;
    string word, wordb;
    cout << "Type in a word\n";
    cin >> word;
    string_length = word.length();
    for (int i=1; i < (string_length+1); i++)
        wordb = wordb + word.at(i);
    if (word == wordb)
        cout << "The word is the same in any direction.\n";
    else 
        cout << "The word is not the same in any direction.\n";
    return 0;
}

很抱歉,如果很明显。

【问题讨论】:

  • i 应该从 0 开始,而不是 1。c++ 中的数组是从零开始的。
  • 不知道怎么了?那我们怎么会有线索呢?!你至少有错误信息吗?
  • 这应该引发异常。阅读它的内容并进行调试。
  • 您是否尝试通过它进行调试?您可以在调试器中对变量设置监视表达式。
  • 你正在按照与word 相同的顺序构建wordb。修复索引(从 0 开始,而不是从 1 开始)后,考虑从 wordend 开始访问字符,然后向后工作。

标签: c++


【解决方案1】:

您不必从word“构造”wordb,可以直接逐一迭代比较字母:

for (int i=0; i < string_length; i++) {
    if letter_in_pos[i] == letter_in_pos[string_length-i-1]
      looks good, do nothing
    else
      break! word is not a palindrome!
}

最后,如果这个词是一个真正的回文,你只需要走到这个词的中间(因为它是对称的)。

【讨论】:

  • 是的,看起来不错,谢谢!
  • @Pavel 所以你应该更正你的答案, letter_in_pos[ i] == letter_in_pos[ string_length - i - 1]
【解决方案2】:

克里斯建议使用std::equal。下面是一个示例:

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

bool is_palindrome(const std::string& s)
{
    return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());
}

int main()
{
    std::string word;
    std::cout << "Type in a word\n";
    std::cin >> word;

    if (is_palindrome(word))
        std::cout << "The word is the same in any direction.\n";
    else 
        std::cout << "The word is not the same in any direction.\n";
}

【讨论】:

    【解决方案3】:

    最简单的方法是写

    if ( word == string( word.rbegin(), word.rend() ) )
    //...
    

    wordb.assign( word.rbegin(), word.rend() );
    
    if ( word == wordb )
    //...
    

    至于您的方法,那么正确的循环将如下所示

    for ( string::size_type i = word.length(); i != 0;  )
        wordb.push_back( word[--i];
    

    for ( string::size_type i = word.length(); i != 0;  )
        wordb += word[--i];
    

    【讨论】:

    • wordb = word; std::reverse(wordb.begin(), wordb.end()); 无需为此编写自己的循环。
    • @tillaert 太复杂了。
    【解决方案4】:

    我知道的最简单的方法是使用简单的逻辑 A[DeltaX]==B[-DeltaX],而 DeltaX

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <locale.h>
    #include <tchar.h>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    int string_length;
    int last_character;
    bool found_mirror = true;
    
    cout << "Type in a word\n";
    cin >> word;
    
    string_length = word.length();
    // if the char count is less that 2 than technically it's true
    if (string_length < 2)
        return true;
    
    last_character = string_length - 1;
    for (int i = 0; i <= (string_length / 2); i++)
    {
        if (word[i] != word[last_character - i])
        {
            found_mirror = false;
            break;
        }
    }
    
    if (found_mirror)
        cout << "The word is the same in any direction.\n";
    else 
        cout << "The word is not the same in any direction.\n";
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2017-02-01
      • 2015-04-02
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多