2 Replies - 275 Views - Last Post: 11 December 2012 - 06:01 AM Rate Topic: -----

#1 captjackAV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-April 12

Ignoring whitespaces in string C++

Posted 11 December 2012 - 02:17 AM

The purpose of this program is to read this special palindrome shown below and check if the lines match. I have to ignore whitespaces, punctuation, and upper and lower case letters should be read as the same. If I just enter the poem, I have no problems. However, if i add several spaces between a word, it will say it is not a palindrome, but my goal is to ignore the spaces and it should say it is a palindrome. Below is my code.


As I Was passing near the jail (line 1 matches line 8)
I met a man, but hurried by. (line 2 matches line 7)
His face was ghastly, grimly pale.
He had a gun I wondered why.
He had A gun I wondered why,
His face was ghastly, Grimly pale,
I met a man, but hurried by.
As I was passing near the jail

#include <iostream>
#include <string>
#include "linkedQueue.h"
#include "myStack.h"
#include "queueADT.h"
#include "stackADT.h"

using namespace std;

void TestCopyConstructor(stackType<int> otherStack);
bool checkifpalindrome(string a, string b, int y, int z);

int main()
{
    linkedQueueType <string> myqueue;
    stackType <string> mystack(200);
    stackType <string> copystack(200);

    mystack.initializeStack();
    myqueue.initializeQueue();

    string p;
    int length = 0;

    cout << "Enter the poem. When finished enter 007." << endl;

while( p != "007")
{
    getline(cin,p); // gets the input from the user
        for(unsigned int i = 0; i < p.length(); i++)
        {
            //remove(p.begin(), p.end(), " "); // removes the white space
                if((p[i] >= 'a') && (p[i] <= 'z')) //derived from the ASCII Table
                {
                p[i] -= 32;
                }

                if(p[i] == ',') // made the program read commas as a period
                {
                p[i] = '.';
                }
        }
            if(p != "007")
            {
            myqueue.addQueue(p); // adds string into the queue
            mystack.push(p); // pushes it into stack
            }
    length++;

}

    cout << endl << endl;
    copystack = mystack; // duplicates mystack into the seperate stack

    int z = 1;

    while(!copystack.isEmptyStack()) //while stack is not empty
    {
        checkifpalindrome(copystack.top(), myqueue.front(), length-1, z);
        myqueue.deleteQueue();
        copystack.pop();
        length--;
        z++;
    }
    cout << endl;

    copystack = mystack;
    cout << endl;

    return 0;
}

void testCopyConstructor(stackType<int> otherStack)
{
    if(!otherStack.isEmptyStack())
    {
        cout << "The other stack is currently not empty." << endl;
        cout << "The top element of the other stack is " << otherStack.top() << endl;
    }
}

bool checkifpalindrome(string a, string b, int y, int z)
{
     if(a  == B)/>/>/>
     {
        cout << "Line " << y << " matches with line " << z << endl;
        return true;
     }
     else
     {
         cout<< "Line "<< y << " does not match with line " << z << endl;
         return false;
     }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Ignoring whitespaces in string C++

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6032
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Ignoring whitespaces in string C++

Posted 11 December 2012 - 03:59 AM

The obvious solution would be to remove all whitespace from the string before you check to see if it's a palindrome, wouldn't it?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5760
  • View blog
  • Posts: 12,573
  • Joined: 16-October 07

Re: Ignoring whitespaces in string C++

Posted 11 December 2012 - 06:01 AM

Ah, I see. Not a proper palindrome, just line checking.

So, um, sanitize the line: string clean(const std::string &) { /* your code here */ }.

I'd investigate isalpha and tolower.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1