>: bool isPalindrome = std::equal(str.begin(), str.end(), str.rbegin()); < >我不认为 StRever()/Cuff>是C++函数。< /P>,c++,C++" /> >: bool isPalindrome = std::equal(str.begin(), str.end(), str.rbegin()); < >我不认为 StRever()/Cuff>是C++函数。< /P>,c++,C++" />

目标:检查用户输入的字符串以确定它是否';这是回文 大家好,我是C++初学者,我正在研究程序,检查用户输入的字符串,以确定它是否是回文。我拥有大部分代码,但仍然存在两个问题:第一个问题出现在函数“bool PalindromeTest()”中,第二个错误是:错误:“strev”未在此范围内声明 #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; } #包括 #包括 使用名称空间std; chars[100]; char-sr[100]; void InputString(); bool回文测试(); void PrintMessage(); int main() { InputString(); 返回0; } 无效输入字符串(int n=0) { 因为这是一个C++问题,一个真正的C++解决方案如何?用 STR 作为 STD::String < /C> >: bool isPalindrome = std::equal(str.begin(), str.end(), str.rbegin()); < >我不认为 StRever()/Cuff>是C++函数。< /P>

目标:检查用户输入的字符串以确定它是否';这是回文 大家好,我是C++初学者,我正在研究程序,检查用户输入的字符串,以确定它是否是回文。我拥有大部分代码,但仍然存在两个问题:第一个问题出现在函数“bool PalindromeTest()”中,第二个错误是:错误:“strev”未在此范围内声明 #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; } #包括 #包括 使用名称空间std; chars[100]; char-sr[100]; void InputString(); bool回文测试(); void PrintMessage(); int main() { InputString(); 返回0; } 无效输入字符串(int n=0) { 因为这是一个C++问题,一个真正的C++解决方案如何?用 STR 作为 STD::String < /C> >: bool isPalindrome = std::equal(str.begin(), str.end(), str.rbegin()); < >我不认为 StRever()/Cuff>是C++函数。< /P>,c++,C++,识别回文的一种快速方法是使用带有两个计数器的循环,例如i和j 从i=0和j=strlen(str)-1(最后一个有效字符)开始,迭代++i和--j,,同时我修复了您的代码: #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; char s[100]; char sr[100]; void In

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

i=0
j=strlen(str)-1
(最后一个有效字符)开始,迭代
++i
--j
,同时我修复了您的代码:

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
chars[100];
char-sr[100];
void InputString();
bool回文测试();
无效打印消息(bool);
int main()
{
InputString();
std::cin.get();
std::cin.get();
返回0;
}
void InputString()
{
int n=0;

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

对于strev
——请看这里:$g++uu.cpp uu.cpp:In函数'bool PalindromeTest()“:uu.cpp:32:error:'strrev'未在此作用域中声明…那么是什么让您认为该函数存在?如果您认为它不存在,为什么要尝试调用它?使用
std::string
而不是char数组会让事情变得更简单我想我们可以通过替换
str.end()来优化它
使用
str.begin()+str.size()/2
是的,这不是为了提高效率而写的。但我认为这是一个优雅的小内衬。如果我需要一个有效的解决方案,我只需循环并比较字符,C风格(编辑:这看起来非常像刚才发布的@MichaelJ的答案)@RetoKoradi-这是一个很好且简单的解决方案,但我怀疑我们的提问者可能还没有准备好理解它。:-@MichaelJ:它解释了自己。如果从头到尾顺序阅读字符串与你开始用r-everse阅读时得到的结果相等,这就是回文。@RetoKoradi-我们的朋友还不理解函数或函数局部变量。迭代器和std::算法将很难。我应该用什么代码来替换它?从哪一行到哪一行???@user3598206-阅读你自己的帖子并尝试找出答案。你写了你发布的代码吗?如果你写了,那么我想你可以找到答案。试试并发布。如果它是错误的,我会帮助你。int main(){InputString();return 0;}void InputString(int n=0){couti在函数palindrome之前清除了i和j,仍然说i和j不是decrealed@user3598206-哦,我犯了一个错误,没有声明这些。这将教会我在不编译代码的情况下编写代码。现在已经修复了。
#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;
}
#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;
}