Palindrome Program

I have an assignment where i have to make a palindrome program. As you can see i have already done the basic palindrome prog, however i am now stuck since the program goes into a infinite loop whenever a word with whitespaces is added.

I have also tried searching for methods online however they do not seem to work for me...

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
void CPali::check()
{
	length = strlen(palin);
	
	for (int i = 0, p = length-1; p>i; i++, p--)
	{
		if (palin[i] == ' ')
		{
			palin[i] = palin[i+1];
			palin[p] = palin[p+1]; 
		}
		

		if (toupper(palin[i]) == toupper(palin[p]))
		{
			
			valid = true;
			
		}
		else
		{
			valid = false;
		}

	}

	if (valid==true)
	{
		cout << "Its a palindrome!" << endl;
	}
	else
	{
		cout << "It's not a palindrome..." << endl;
	}

};
I don't see how that for-loop will go into an infinite loop but I did see a problem with one of your if-statements:

if (palin[i] == ' ') Should be if (palin[i] == " ")
The way you were doing it, it will be checking the ascii value of the (' ') character which is 32 and since that is not what you want, you will never get a match there.


Also since you are looking for a palindrome, checking for matching letters every time in the for-loop is not a good idea because you could get false positives.

ex. if you have the string:

whaagw

With the way your code is set up, it will say that that string is a palindrome when clearly it is not. So what you should be checking for instead is if the characters on either end do not match and when this is true, you should break out of the for loop and return false.
if (palin[i] == ' ') Should be if (palin[i] == " ")
The way you were doing it, it will be checking the ascii value of the (' ') character which is 32 and since that is not what you want, you will never get a match there.


The if condition was correct. palin[i] is a char. ' ' is a char. " " is not. The body of code executed when the if is entered is not correct, however. And both indices should be checked for spaces.

Here's an alternate version:

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
#include <cstring>
#include <cctype>
#include <iostream>

bool isPalindrome( char const * str )
{
    std::size_t length = std::strlen(str);
	
    char const* left = str ;
    char const* right = str+length-1;

    while ( left < right )
    {
        if ( std::toupper(*left) != std::toupper(*right) )
        {
            if ( *left == ' ' )
                ++left ;
            else if ( *right == ' ' )
                --right ;
            else
                return false ;
        }
        else
            ++left, --right ;
    }

    return true ;
}

int main()
{
    char buffer[1000] ;

    std::cout << "Enter the string to be evaluated.\n" ;
    std::cin.getline(buffer, sizeof(buffer)) ;

    if ( isPalindrome(buffer) )
        std::cout << "Palindrome!\n" ;
    else
        std::cout << "Not a palindrome!\n" ;
}
Last edited on
Try this

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

#include <iostream>
#include <string>
using namespace std;

int main() {

  string word;
  int len = 0;

  cout << "Enter a word." << endl;

  cin >> word;

  for (int i = 0;i < word.length(); ++i) {

    if (word[i] == word[word.length() - 1 - i]) 
      ++len;
    else break;

  }

  if (len == word.length()) 
    cout << "It is a palindrome!" << endl;
  else
    cout << "It is not a palindrome!" << endl;

  cin.get();

  return 0;
}


This is untested yet. Hope you get the idea.
This is untested yet. Hope you get the idea.


Besides being inefficient (it does twice as many comparisons as is necessary) it completely ignores the requirement to handle spaces correctly.
My five cents.


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
#include <iostream>
#include <string>
#include <cctype>

int main()
{
	std::string s;

	while ( true )
	{
		std::cout << "Enter a string: ";
		std::getline( std::cin, s );

		if ( s.empty() ) break;

		auto first = s.begin(), last = s.end();
		bool isPalindrome = true;

		while ( isPalindrome && first != last )
		{
			if ( isblank( *first ) )
			{
				++first;
			}
			else if ( !isblank( *--last ) )
			{
				isPalindrome = *first == *last;
				if ( first != last ) ++first;
			}
		}

		std::cout << "\"" << s << "\" is " 
		          << ( isPalindrome ? "" : "not " ) << "a palindrome"
			  << std::endl;
	}

	return 0;
}
Last edited on
This is a duplicate of a thread that is marked as solved:
http://www.cplusplus.com/forum/beginner/92100/#msg495116
Topic archived. No new replies allowed.