Need help with writing basic code to determine if input is a palindrom

So I am writing a basic code that asks the user for input and then determines if the input is a palindrome or not. So far I have the code below. I am trying to store the word in an array and then have that name copied to another box. It then compares each of the arrays in the boxes. Im trying to get one to start from the first letter and go forward and the other to start from the last letter and go backwards. If all the letters are the same it should trigger bool to equal true and complete the loop. If bool is false it should just put out "not palindrome" or something along those lines. I just need help with the do-while loop because I don't have it setup correctly. Check it out.

#include <iostream>

using namespace std;

#include <string.h>

void main()
{
const long MaxChar(80);
char name1[MaxChar];
char name2[MaxChar];
int i;
int b;
bool isPalindrome;

cout << "Type something: ";

cin.getline(name1, MaxChar + 1);

if (cin.fail())
{
cin.clear();
cin.ignore(80, '\n');
}

cout << "You entered: " << name1 << endl;

strcpy(name2, name1);

i = -1; //not using right now
b = -1; //not using right now

do {

name2['\0' - 1] = name1['0' + 1];

isPalindrome = true;

} while (!isPalindrome);

if (isPalindrome = true);
cout << "It is true" << endl;
if (isPalindrome = false);
cout << "It is false" << endl;

}
There are a few issues, I think. First is a syntactical issue:

 
if( isPalindrome = true);


This sets the value of your boolean isPalindrome to true, which is not what you want. You want to check if the boolean is true, and to do that you need to replace the assignment operator "=" with "==", which checks if the two are equal.

Next, your control structure of the dowhile loop is off. You only want to exit the do while loop if you know for certain the string is or is not a palindrome. The way you have it right now, it only leaves the do-while loop if isPalindrome is false, which will never happen and the loop will continue indefinitely.

I suggest something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool isPalindrome = true;

int i = 0;
int namelength = sizeof(name1/sizeof(char)); //this should tell you the length of the string, i.e. the number of letters
while( i < namelength ) 
   {
      if(name[i] != name[namelength - i]) //Checks ith letter with n-ith letter
         {
            isPalindrome = false;
            break;
         }
   }

if ( isPalindrome == true )
   {
      cout << "It is true" << endl;
   }
else
   {
      cout << "It is false" << endl;
   }


Notice that you don't really need two strings to do this. Also, the loop actually contains twice as many steps as it needs to since you should only have to check half the string (checking the second half is equivalent to the first). But this should work.
Last edited on
int namelength = sizeof(name1/sizeof(char)); //this should tell you the length of the string, i.e. the number of letters
Not really. it will return the length of the array, which equals MaxChar, but the string itself may be shorter. What the OP needs is strlen, or std::string.
I am trying what both of you recommended but i just keep getting false all the time. Is it maybe adding the 1 to i before it checks? if so, how do i stop it?

I set namelength = strlen (name1)

and then used

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (i < namelength)
		{
		i++;
		if (name1[i] == name2[namelength - i])
			{
			isPalindrome = true;
			}
		else (isPalindrome = false);
		break;
		}

	if (isPalindrome == true)
		{
		cout << "It is true" << endl;
		}
	else {
		cout << "It is false" << endl;
		}
Last edited on
Consider this
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstring>
#include <iostream>

int main()
{
    char str[10] = "hello"; // 'h' 'e' 'l' 'l' 'o' '\0'
    int size = std::strlen(str); // == 5 because terminator is not counted
    int i = 0; // on your first loop iteration i == 0
    if(str[size - i] == '\0') // str[5 - 0] == str[5] == '\0'
        std::cout << "str[5] == '\\0'" << std::endl;
    return 0;
}


As you can see you compare the first character in the string with the string terminator, not the last character.
Last edited on
Topic archived. No new replies allowed.