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

#1 MikeAbyss   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • 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);
        }
    }
    cout << "Its a paladrone";
    getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome

#2 MikeAbyss   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#3 Rrroobydoo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 MikeAbyss   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 MikeAbyss   User is offline

  • D.I.C Head
  • member icon

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

Re: Palindrome

Posted 05 November 2008 - 01:13 PM

Typo, it should be all "+1" not "-1"
Was This Post Helpful? 0
  • +
  • -

#6 amyceres   User is offline

  • D.I.C Head

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

Re: Palindrome

Posted 11 October 2012 - 10:59 AM

what about the length? not Initialized?
Was This Post Helpful? 0
  • +
  • -

#7 sharoncool   User is offline

  • New D.I.C Head

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

Re: Palindrome

Posted 23 November 2015 - 04:57 PM

View Postamyceres, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1