Search code examples
c++stackqueuepalindromeistringstream

read strings from txt file and stop at first line in c++


I have to use stack and queue to check if it is palindrome from the txt file for my homework.

************Txt File**************

Did I say you never say "never say never"? You say I did.

Did I say you never say "never"?

Are you glad you are king?

King, are you glad you are king?

Fall leaves after leaves fall.

Says Mom, "What do you do?" -You do what Mom says.

Says Mom, "What do you do?" -You do what Mom does.

You know, I did little for you, for little did I know you.

You know, I did little for you, since little did I know you.

First Ladies rule the State.

Escher, drawing hands, drew hands drawing.

You can cage a swallow, can't you?

First Ladies rule the State, and state the rule: "ladies first".

Blessed are they that believe they are blessed.

You can cage a swallow, can't you, but you can't swallow a cage, can you?

Mind your own business: Own your mind.

Rode, and rode, and rode, and rode, and rode, and rode, and rode!

Clatter and hum and crunch, and crunch and hum and clatter.

Mind your own business.

All for one, and one for all!

Escher, drawing hands, drew hands drawing Escher.


pretend there is no empty space between each lines. So, after the first line, there is no empty line, the second sentence follows.

I used istringstream to stop at first line, but there is an error with the output. The last word of each sentence print out twice for some reason.

Here is my code,

void getData(Stack<string> &s, Queue<string> &q)
{
    ifstream readFile;
    string temp;
    string temp1;
    Stack<string> a[24];
    Queue<string> b[24];
    int j = 0;
    //int b = 0;

readFile.open("test_word_plndrms.txt");

if(!readFile)
{
    cout << "Error opening the file" << endl;
    exit(1);
}
else {
    while(!readFile.eof()) {
            getline(readFile, temp);
            for (size_t i = 0; i < temp.length(); ++i) {
                if (ispunct(temp[i]))
                    temp.erase(i--, 1);
                if(isupper(temp[i]))
                    temp[i] = tolower(temp[i]);
            }
        istringstream ss(temp);
            while(ss) {
                ss >> temp1;
                cout << temp1 << endl;
                a[j].push(temp1);
                b[j].enqueue(temp1);
            }
        j++;

    }

}

}

And this is my output,

did i say you never say never say never you say i did did did i say you never say never never are you glad you are king king king are you glad you are king king fall leaves after leaves fall fall says mom what do you do you do what mom says says says mom what do you do you do what mom does does you know i did little for you for little did i know you you you know i did little for you since little did i know you you first ladies rule the state state escher drawing hands drew hands drawing drawing you can cage a swallow cant you you first ladies rule the state and state the rule ladies first first blessed are they that believe they are blessed blessed you can cage a swallow cant you but you cant swallow a cage can you you mind your own business own your mind mind rode and rode and rode and rode and rode and rode and rode rode clatter and hum and crunch and crunch and hum and clatter clatter mind your own business business all for one and one for all all escher drawing hands drew hands drawing escher escher Program ended with exit code: 0

What I am trying to do is read the first line, and put each word such as "did" "i" "say" "you" "never" "say" "never" "say" "never" "you" "say" "i" "did" and push them to stack and queue. And then, pop each strings and if popped string is different, it is not a palindrome sentence, if it is all same, it is a palindrome sentence.

Can someone give me any suggestion for this homework? Thank you.


Solution

  • The reason why it prints the last word twice is because of the statement:

    while(ss)
    {
       ...
    }
    

    The while loop takes an extra loop because ss is still valid. You can probably clear it and check if temp1 is empty.

    while (ss)
    {
        ss >> temp1;
    
        if (temp1.empty())
        {
            break;
        }
    
        cout << temp1 << endl;
        a[j].push(temp1);
        b[j].enqueue(temp1);
        temp1.clear();
    }