I"m creating a function which returns true or false on whether a word is a palindrome, which also disregards spaces in the string.
For example "taco cat" would be considered a palindrome.
I'm taking in the string to be checked through getline, however wordA.length is always returning 0, and I'm not sure what to fix.
Any help would be appreciated.
Thanks!
bool checkPalindrome() {
string wordA;
cout << "The word is: " << endl;
getline(cin, wordA);
cin.clear();
cin.ignore(10000, '\n');
//This outputs 0
cout << wordA.size() << endl;
char* wordB = new char[wordA.length()+1];
int j = 0;
for (unsigned int i = wordA.length() - 1; i > 0; i--) {
wordB[j] = wordA[i];
j++;
}
int k = 0;
for (unsigned int i = 0; i < wordA.length(); i++) {
if (wordA[i] == ' ') {
continue;
}
while (wordB[k] == ' ') {
k++;
}
if (wordA[i] != wordB[k]) {
cout << "It's not a palindrome" << endl;
delete[] wordB;
return false;
}
k++;
}
cout << "It is a palindrome" << endl;
delete[] wordB;
return true;
}
int main()
{
int response;
cout << "Enter test case " << endl << endl;
cout << "0: Sort" << endl;
cout << "1: Get Permutations" << endl;
cout << "2: Check Permutations" << endl;
cout << "3: Check Permutation Palindrome" << endl << endl;
cout << "Selection: ";
cin >> response;
switch (response) {
case 0:
mergeCase();
break;
case 1:
permutationCase();
break;
case 2:
checkPermutation();
break;
case 3:
checkPalindrome();
break;
}
}