對不起,如果這看起來真的不好用,而且事實是,我現在只研究了一個月左右。我似乎無法讓這個工作正確,想知道你們中的任何一位能否幫助我。該程序曾經編譯過,只是說輸入的文字是「非迴文」,即使它們是。使用數組和無指針創建Palindrome字符串檢查器C++
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
bool isPal (char[], int);
int main()
{
const int size = 10;
int flag = 0;
char arr[size];
cout << "Enter Word\n";
cin.getline(arr,10);
flag = isPal(arr, size);
if (flag == true)
cout << "Word is Palindrome\n";
else
cout << "Not Palindrome\n";
return 0;
}
bool isPal (char arr [],int size)
{
int q = 0;
char arr2[size];
for (int i = 0;i < size - 1; i++)
{
arr2[i] = arr[9 - q]; //here I attempt to reverse arr1 and assign each element to arr2
q++;
}
for (int j = 0; j < size - 1; j++)
{
if (arr [j] != arr2[j])
return false;
}
return true
}
遺憾地告訴你,但你的程序是使用指針。 'isPal'裏面的'arr'就是這個。 – chris 2014-10-29 18:49:58
我還沒有研究過指針是什麼,所以我想我一直在不知不覺中使用它們。 – chris 2014-10-29 18:53:21
這可能會有幫助。 http://stackoverflow.com/questions/9106295/palindrome-program。和http://stackoverflow.com/questions/3285432/to-find-if-a-given-string-is-palindrome-or-is-not-palindrome – 2014-10-29 18:55:13