如何在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