Hi Everyone I'm actually having problem with a console palindrome app I created, the goal is for the app to check a given word regardless of length, then check for every possible palindrome phrase or word inside that word e.g if the user enters a word like "rocktoflowracecar", now the app should be able to detect the palindrome inside that word i.e racecar or any other type that conforms with palindrome rules. But my problem is with the if statement, it doesn't seems to work even at the point when the condition is true.
#include <iostream>
usingnamespace std;
string ReverseWord (string Word)
{
string Rword;
for (int Start = 0; Start <= Word.length(); Start++)
{
Rword += Word[Word.length() - Start];
}
return Rword;
}
bool isEqual (string RWord, string testWord){
//Test Check to see the possible palindrome
cout <<"Test Check [" << RWord<<" == " <<testWord<<"]\n";
if (RWord==testWord){
/* Also tried to use the tenary control but it still print the right operand which denote false
-> (get==go) ? printf("works") : printf("none");*/
cout <<"This word conforms to the rule of palindrome ";
returntrue;
}
elsereturnfalse;
}
int main(int argc, char *agrv[])
{
string getWord, checkWord;
unsignedshortint wordLength;
getline(cin, getWord);
wordLength = getWord.length();
for (int startPos = 0; startPos < wordLength; startPos++)
{
for (int nextPos = 0; nextPos < wordLength; nextPos++)
{
checkWord = getWord.substr(startPos, wordLength-nextPos);
//Combining Function call.
( isEqual( ReverseWord(checkWord), checkWord));
}
}
// cout << PalindCount; Not included yet because of the if statement not working properly.
}
The last index of string Word is Word.length()-1 (because the first index is 0). On both of lines 8 and 10 you are going to go beyond string bounds.
Other things:
- it's not a pretty way of reversing anything;
- not clear whether you are going to allow duplicates in your final list of palindromes; as it stands you could get palindromes several times;
- note that each individual letter will, itself, form a (trivial) palindrome.