检查字符串是否为回文。 C++
这有什么问题?
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(string str)
{
char temp[1000];
int len=str.length();
int i=0;
while(len)
{
len--;
temp[i++]=str[len];
}
temp[i]='\0';
if (strcmp(str,temp)==0)
return true;
else
return false;
}
What is wrong in this?
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(string str)
{
char temp[1000];
int len=str.length();
int i=0;
while(len)
{
len--;
temp[i++]=str[len];
}
temp[i]='\0';
if (strcmp(str,temp)==0)
return true;
else
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如下所示:
亮点:
std ::等等
std :: string_view
<
Here's how you do it:
Highlights:
std::equal
std::string_view
std::string_view::rbegin
您的代码远比应有的要复杂得多。
另一个实现:
strcmp()
您正在使用的功能,仅接受C-string作为参数,但是您正在传递C ++字符串。同样,如果字符串str
长度大于1000,则它将无法使用。Your code is far more complex than it should be.
Another implementation:
strcmp()
function that you are using, accepts only c-string as arguments, but you are passing C++ string. Also it won't work ifstring str
length is more than 1000.