-1

Hi this is my code for the palindrome program:

void palindrome()
{
    string input;
    bool checkInput, palindrome;
    palindrome = true;
    do
    {
        checkInput = false;
        cout << "Enter a word, phrase or sentence :\n";
        getline(cin, input);

        for (unsigned int i = 0; i < input.size(); i++)
        {
            if (input[i] < 65 || input[i] > 90 && input[i] < 97 || input[i] > 122)
            {
                checkInput = true;
            }
        }



    } while (checkInput);

    for (unsigned int i = 0, j = input.size() - 1; i < input.size(); i++, j--)
    {
        if (input[i] != input[j] && input[i] + 32 != input[j] && input[i] - 32 != input[j])
        {
            palindrome = false;
            break;
        }
    }

    if (palindrome)
    {
        cout << "\n\nTo consider only letters and digits:\n";
        cout << input << "\nYes, it is palindrome!\n";
        cout << "\t\t Press <Enter> key back to menu";
        fflush(stdin);
        cin.get();
    }
    else
    {
        cout << "\n\nTo consider only letters and digits:\n";
        cout << input << "\nNOPE, it's not palindrome\n";
        cout << "\t\t Press <Enter> key back to menu";
        fflush(stdin);
        cin.get();
    }
}

and when my input is racecar it reads and says it is a palindrome, but when my input is race car (with a space) it doesn't read and it says its not a palindrome. My intend is to ignore all the spaces. Any help will be much appreciated! Thanks in advance!

**editted so i switched my cin >> input to getline(cin, input) and it doesnt let me input my words or phrases

  • One tip to make your code more readable is to use character literals instead of numbers e.g. `input[i] < 'A'` instead of `input[i] < 65`. – Simon Mar 17 '15 at 08:27
  • @corkiiunranked , There are 2 answers that solve your problem and follow the tip from Simon. – Jagannath Mar 17 '15 at 08:31
  • I would rather use `isalnum` from `` instead of the manual check. – martin Mar 17 '15 at 08:37

3 Answers3

4

Maybe it will work if you first remove all whitespaces after reading the Input?

#include <algorithm>

str.erase(remove_if(str.begin(), str.end(), isspace), str.end());

Whitespaces are not in die ASCII values you are checking, so the whileloop ends at the first whitespace.

J-Eibe
  • 152
  • 4
  • 12
1

The Problem

The palindrome is a word that is spelled backwards and forwards the same. Therefore, you can be sure that, going from the outside in, the letters need to be the same until you're examining the same letter (the total number of letters are odd) or the letter-searching things/examiners/markers (let's call them iterators) criss-cross.

How do you examine pair of letters from outside to inside? By using an index loop from the first to last position in tandem with a last-to-first index loop.

How You Do it (The Implementation)

Let's pretend we have two variables to act as the iterators, i and j. i will move forward while j will move backward. They will start at opposite ends:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    //This is where our word will be.
    std::string str;
    
    //Get input
    std::cout << "Input your word, please!" << std::endl;
    std::getline(std::cin, str);
    
    //Let's use std::erase to take away our whitespaces from the
    //C++11 library <algorithm>
    str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
    
    //Initialize i and j, our iterators
    //I use auto because the iterator type is long. It's a reason why auto was invented.
    auto i = str.begin();
    auto j = str.end() - 1;
    //You see, str.end() is actually the END, and not the last letter.
    //That's why it has a -1.
    
    bool is_palindrome = true;
    
    while (i < j) //While the two haven't crossed yet
    {
        //This std::cout shows you what i and j are at each repeat
        std::cout << "i = " << *i << " ||| j = " << *j << std::endl;
        
        //If the two characters marked by the two iterators are not equal
        if (*i != *j)
        {
            is_palindrome = false;
            break;
        }
        else
        {
            //Let's continue.
            ++i;
            --j;
        }
    }
    
    //If the loop gets to this point successfully, it's a palindrome.
    if (is_palindrome)
        std::cout << str << " is a palindrome!" << std::endl;
    else
        std::cout << str << " is not a palindrome." << std::endl;
        
    return 0;
}

Hopefully this will help you. Remember to compile with -std=c++11 for C++11 features!

Community
  • 1
  • 1
CinchBlue
  • 6,046
  • 1
  • 27
  • 58
0

See if this works.

    for (unsigned int i = 0, j == input.size() - 1; i < input.size();)
    {
        //Ignore spaces
        if (input[i] == ' ')
        {
           ++i; continue;
        }
        if (input[j] == ' ')
        {
           --j;continue;
        }

        //Automatic palindrome analysis 
        if (input[i] != input[j])
        {
            palindrome = false;
            break;
        }
        ++i;--j;
    }
CinchBlue
  • 6,046
  • 1
  • 27
  • 58
Jagannath
  • 3,995
  • 26
  • 30