palindrome

can you please help me to create a program that will test the program if it is a palindrome or not this is the sample output..


Enter a string: MADAM
MADAM is a PALINDROME!!!!

Enter a string: KRISTINA
KRISTINA is not a PALINDROME!!!!

do you want to continue? y/n

Last edited on
Tailor the following for your need
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 <algorithm>

using namespace std;

int main()
{
                const string str1("madam");
                string str2(str1.begin(),str1.end());	
                cout << "Actual String: " << str1 << endl;    
                reverse (str2.begin(), str2.end());
                cout << "reverse:       " << str2 << endl;

                if(!str1.compare(str2))
	               cout << "String is a palindrome" << endl;
                else
	               cout << "String is not a palindrome" << endl;

                return 0;
}	
It isn't a whole lot of help to 1) give away an answer 2) give an answer she cannot use.

Kristina, please stop spamming the forum. You are actually less likely to get help when you do that than with a single, well-thought post.

To test whether or not you have a palindrome, you will need to consider the indices into the array of characters that make the string. For example:

0 1 2 3 4 5 6 7 : index
K R I S T I N A : letter

To check for a palindrome requires a little (teeny tiny) bit of math. "KRISTINA" has eight letters, so the indices are zero to seven.

Use a loop and two counters. One counter goes from zero to seven. One counter goes from seven downto zero. Inside the loop just check to see if the letter at the first index matches the letter at the second index. If it doesn't, then the string is not a palindrome.

Extra credit hint: You don't actually have to stop looping when the counters hit seven and zero (respectively). (You can stop sooner.)

Good luck!
Last edited on
thank you for answering my request!!!
Topic archived. No new replies allowed.