【问题标题】:Objective: To examine a user-entered string to determine if it's a palindrome [duplicate]目标:检查用户输入的字符串以确定它是否是回文[重复]
【发布时间】:2014-05-03 03:09:41
【问题描述】:

大家好,我是 C++ 初学者,我正在开发程序来检查用户输入的字符串以确定它是否是回文。我有大部分代码,但仍然有两个问题,第一个是 In function ‘bool PalindromeTest()’,第二个错误是:error: ‘strrev’ is not declared in this scope

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

char s[100];
char sr[100];

void InputString();
bool PalindromeTest();
void PrintMessage();

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

void InputString(int n=0)
{
       cout<<"Input the string you wish to be tested";
       cin.getline(s, 100);
       if(strlen(s)>n)
          PalindromeTest();
}

bool PalindromeTest()
{
       bool rvalue = true;
       strcpy(sr, s);
       strrev(sr);
       if(strcmp(s, sr) == 0)
       {
              rvalue=true;
              PrintMessage();
       }
       else
       {
          rvalue=false;
          PrintMessage();
    }
void PrintMessage(bool rvalue)
{
       if(true == rvalue)
         cout<<"The entered string IS a palindrome"<<endl;
       else
         cout<<"The entered strins IS NOT a palindrome"<<endl;
}

【问题讨论】:

  • 对于strrev -- 看这里:stackoverflow.com/questions/8534274/…
  • ]$ g++ uu.cpp uu.cpp: In function ‘bool PalindromeTest()’: uu.cpp:32: error: ‘strrev’ is not declared in this scope
  • ...那么是什么让您认为该功能存在?
  • 如果你认为它不存在,你为什么要调用它?
  • 使用 std::string 而不是 char 数组,您自己会轻松很多

标签: c++


【解决方案1】:

既然这是一个 C++ 问题,那么真正的 C++ 解决方案怎么样? strstd::string:

bool isPalindrome = std::equal(str.begin(), str.end(), str.rbegin());

【讨论】:

  • 我想我们可以通过将 str.end() 替换为 str.begin() + str.size() / 2 来优化它
  • 是的,这不是为了提高效率而写的。但我认为它是一个优雅的小班轮。如果我需要一个有效的解决方案,我会循环并比较字符,C 风格(编辑:这看起来很像 @MichaelJ 刚刚发布的答案)。
  • @RetoKoradi - 这是一个很好且简单的解决方案,但我怀疑我们的提问者可能还没有准备好理解它。 :-)
  • @MichaelJ:它解释了自己。如果从 beginend 的顺序读取字符串 equalbeginr-everse 中阅读。 ;)
  • @RetoKoradi - 我们的朋友还不了解函数或局部变量。迭代器和 std:: 算法会很困难。
【解决方案2】:

我认为strrev() 不是 C++ 函数。

识别回文的快速方法是使用带有两个计数器的循环,例如 ij

i=0j=strlen(str)-1(最后一个有效字符)开头,然后迭代++i--jwhile i&lt;j

如果在任何迭代中,str[i] != str[i],那么它不是回文,所以你可以返回 false。 如果它到达循环的末尾而没有发现任何差异,那么它是一个回文。

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

【讨论】:

  • 这个我应该用什么代码替换?从哪一行到哪一行??
  • @user3598206 - 阅读您自己的帖子并尝试找出答案。您是否编写了发布的代码?如果你这样做了,那么我认为你可以解决这个问题。试一试并发布。如果错了我会帮你的。
  • int main() { InputString();返回0; } void InputString(int n=0) { coutn)回文测试(); } bool Palindrome(const char *str) { int len = strlen(str); for (i=0, j=len-1; i
  • 我在回文函数之前清除了 i 和 j,但仍然说 i 和 j 没有减少
  • @user3598206 - 糟糕。我犯了一个错误,没有声明那些。这将教我在不编译的情况下编写代码。现在已修复。
【解决方案3】:

我修复了你的代码:

#include <iostream>
#include <cstring>
#include <cstdio> 
    #include <cstdlib>

using namespace std;

char s[100];
char sr[100];

void InputString();
bool PalindromeTest();
void PrintMessage(bool);

int main()
{
    InputString();

    std::cin.get() ;    
    std::cin.get() ;
    return 0;
}

void InputString()
{
    int n = 0;
    cout<<"Input the string you wish to be tested \n";
    cin.getline(s, 100);
    if(strlen(s)>n)
        PalindromeTest();
}

bool PalindromeTest()
{
    bool rvalue = true;
    strcpy(sr, s);
    strrev(sr);
    if(strcmp(s, sr) == 0)
    {
        rvalue=true;
        PrintMessage(rvalue);
    }
    else
    {
        rvalue=false;
        PrintMessage(rvalue);
    }
    return false ; // ??
}
void PrintMessage(bool rvalue)
{
    if(true == rvalue)
        cout<<"The entered string IS a palindrome"<<endl;
    else
        cout<<"The entered strins IS NOT a palindrome"<<endl;
}
  1. 前向声明中的方法签名(名称、参数数量及其类型)必须与其定义中的签名相同。(查看 InputString 和 PrintMessage 的默认和固定版本的声明和定义)。

  2. n 未定义。

  3. 类型不是 void 的方法必须返回一个值,但在您的情况下,PalindromeTest 不需要返回任何内容。

这里有一个更好的版本:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

char s[100];
char sr[100];

void InputString();
bool PalindromeTest();
void PrintMessage(bool);

int main()
{
    InputString();

    std::cin.get() ;    
    std::cin.get() ;
    return 0;
}

void InputString()
{
    cout<<"Input the string you wish to be tested \n";
    cin.getline(s, 100);
    PrintMessage(PalindromeTest());
}

bool PalindromeTest()
{
    strcpy(sr, s);
    strrev(sr);
    if(strcmp(s, sr) == 0)
    {
        return true ;
    }
    else
    {
        return false ;
    }
}
void PrintMessage(bool rvalue)
{
    if(rvalue)
        cout<<"The entered string IS a palindrome"<<endl;
    else
        cout<<"The entered strins IS NOT a palindrome"<<endl;
}

【讨论】:

    【解决方案4】:

    您需要在调用 PrintMessage() 时将右值作为参数传递。 想想,会有帮助的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2021-03-25
      • 2019-03-09
      • 1970-01-01
      相关资源
      最近更新 更多