# Palindrome

Page 1 of 1

## 6 Replies - 4736 Views - Last Post: 23 November 2015 - 04:57 PM

### #1 MikeAbyss

Reputation: 11
• Posts: 56
• Joined: 09-July 07

# Palindrome

Posted 10 July 2007 - 12:08 AM

Description: Finds out if a word is a palindrome (A word spelled backwards and forwards the same way) This can also be used for numbers
```#include <iostream.h>
#include <string.h>
#include <conio.h>
using namespace std;

int main()
{
string Word;

cout << "Enter a word: ";
cin >> Word;

for (int x=0; x < Word.length()-1; x++){

if (tolower(Word[x]) != tolower(Word[Word.length()-(x+1)])){
cout << "It is not a paladrome";
getch();
exit(0);
}
}
getch();
}

```

Is This A Good Question/Topic? 0

## Replies To: Palindrome

### #2 MikeAbyss

Reputation: 11
• Posts: 56
• Joined: 09-July 07

## Re: Palindrome

Posted 10 July 2007 - 12:08 AM

Description: Finds out if a word is a palindrome (A word spelled backwards and forwards the same way) This can also be used for numbers
```#include <iostream>
#include <string>
using namespace std;

int main()
{
string word;
bool palindrome;

cout << "Enter a word: ";
cin >> word;

for (int x=0; x < word.length()-1; x++)
{
if (tolower(word[x]) != tolower(word[word.length()-(x+1)]))
{
palindrome = false;
break;
}
else
palindrome = true;
}

if (palindrome)
cout << "It is a palindrome";
else
cout << "It is a not palindrome";

return 0;
}
```

### #3 Rrroobydoo

Reputation: 0
• Posts: 2
• Joined: 05-November 08

## Re: Palindrome

Posted 05 November 2008 - 12:53 PM

Mike, Thanks for the snippet. It got me "over the hump" to the braincramp i was having! Why did you convert all the letters to lowercase? Could you explain the mechanics behind 'word[word.length() -(x+1)]' within the For loop? Thx, Scott

### #4 MikeAbyss

Reputation: 11
• Posts: 56
• Joined: 09-July 07

## Re: Palindrome

Posted 05 November 2008 - 01:12 PM

Q: Why did you convert all the letters to lowercase? A: Lets say the user enters in Otto, because 'O' is not the same as 'o' the program will say its not a palindrome, thus by lowering it, it will fix that problem Q:Could you explain the mechanics behind 'word[word.length() -(x+1)]? A: This one is going to be a bit confusing for you. I'll try my best to explain it (tolower(word[x]) != tolower(word[word.length()-(x+1)])) So lets say the user enters in otto, a string by definition is an array of characters, so index [0] = 'o' [1] = 't' [2] = 't' [3] = 'o' ( tolower (word[x]) != tolower ( word [ word.length()- (x+1) ] ) ) Now lets sub in the numbers so it makes sense when x is 0. ( tolower ( word[0] ) != tolower ( word [ 4 - (0 - 1) )] ) Now the index of word[0] is 'o', if that is NOT equal to the index of word [4 - (0 + 1)] then it is not a palindrome. (Word[ 4 - ( 0 -1)] equals to word[3] and if you look back to the array of characters, word[3] is 'o' Those lines basically compares each letters with one another, one moves up and the other moves down.

### #5 MikeAbyss

Reputation: 11
• Posts: 56
• Joined: 09-July 07

## Re: Palindrome

Posted 05 November 2008 - 01:13 PM

Typo, it should be all "+1" not "-1"

### #6 amyceres

Reputation: 0
• Posts: 88
• Joined: 11-October 12

## Re: Palindrome

Posted 11 October 2012 - 10:59 AM

what about the length? not Initialized?

### #7 sharoncool

Reputation: 0
• Posts: 5
• Joined: 22-November 15

## Re: Palindrome

Posted 23 November 2015 - 04:57 PM

amyceres, on 11 October 2012 - 10:59 AM, said:

what about the length? not Initialized?

well according to my knowledge length() here is a function ........u can either use dat or u can use string length
```int b
b=strlen(word)

```

this was another method
but header file of string.h is required