如何在C++中创建Palindrome函数?我使用了2种函数类型(bool和void)。 这是我的代码到目前为止(我真的很感激任何帮助,为什么我的代码不工作?)谢谢!C++ Palindrome布尔函数(使用指针)
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>
using namespace std;
void reverse(char *);
bool isPalindrome(char *);
int main()
{
char a[10];
cout << "string ";
cin.getline(a, 10);
if (palindrome(a))
cout << "true";
else
cout << "false";
return 0;
}
void reverse(char *s)
{
char *point = s;
int i, min;
for (i = 0; *point != '\0'; i++)
point++;
min = i;
point--;
for (i = min; i > 0; i--)
{
cout << *point--;
}
}
bool ispalindrome(char *s)
{
bool status;
char *original = s;
char *point = s;
reverse(point);
for (int i = 0; point != '\0'; i++)
{
if (point[i] == original[i])
status = true;
else
status = false;
}
return status;
}
因此,在我阅读所有这些代码之前......您是否收到错误消息或答案不正确?添加该细节,然后对其进行阐述。如果这是一个错误信息,请给它。如果它不正确,请显示一些示例输入和输出。 – Segfault
感谢您的回复。它给了我这个错误:线程1:EXC_BAD_ACCESS(代码= 1。地址= 0x7fffSfc87000) – ptaal
当你设置status = false你应该打破循环(应该是一段时间,而不是一个),因为如果中间的2个字母是等于它返回true,即使其他字母都是不同的 – hidrargyro