Help am having problem with this c++ Palindrome app

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace 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 ";
	   return true;
	}
	else return false;
}

int main(int argc, char *agrv[])
{

	string getWord, checkWord;
	unsigned short int 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.

}



Last edited on
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.
Last edited on
there are actually 3 palindromes lurking inside rocktoflowracecar of size > 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::string myString = "rocktoflowracecar";
    std::vector <std::string> palindromes{};
    for (size_t i = 0; i < myString.size(); ++i)
    {
        for (size_t j = 0; j < myString.size() - i; ++j)
        {
            std::string temp =  std::string{myString.cbegin() + i, myString.cend() - j};
 		//using ctor #6 from this link: http://en.cppreference.com/w/cpp/string/basic_string/basic_string
            if (temp == std::string(temp.rbegin(), temp.rend()))//reverse iterators 
            {
                if(temp.size() > 1) palindromes.push_back(temp);
            }
         }
    }
    if(palindromes.empty()){std::cout << "No palindromes found \n";}
    else {for (const auto& elem : palindromes){std::cout << elem << " ";}}
}

but for each substring if you don't want to check the sub-sub strings you can eliminate the inner loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::string myString = "rocktoflowracecar";
    std::vector<std::string> palindromes{};
    for (size_t i = 0; i < myString.size(); ++i)
    {
        if((myString.substr(i) == std::string{myString.crbegin(), myString.crbegin() + myString.size() - i})
           //using ctor #6 from this link: http://en.cppreference.com/w/cpp/string/basic_string/basic_string
           && (myString.substr(i).size() > 1))//overlooks last letter as palindrome if last == first letter
       {
           palindromes.push_back(myString.substr(i));
       }
    }
   if(palindromes.empty()){std::cout << "No palindromes \n";}
    else { for (const auto& elem : palindromes) std::cout << elem << " "; }
}

Topic archived. No new replies allowed.