Comparing strings with a queue

Hello, just started working with queues and need a push in the right direction. We only briefly went over the topic in class and it's an extra credit assignment, so I'd like to complete it and understand what's going on.

Write a program that reads two sentences and reads them into two separate queues. The program should then determine whether the sentences are identical by comparing characters between the two. When two non-identical characters are encountered, the program should display a message indicating that the sentences are not the same. If both queues contain the same set of characters, a message should be stated that they are identical.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <queue>
using namespace std;

int main()
{
    queue<string> str1, str2;
    
    string s;
    
    cout << "Enter string #1: ";
    cin >> s;
    
    str1.push(s);
    
    cout << "Enter string #2: ";
    cin >> s;
    
    str2.push(s);
    
    // both queues have their respective strings. what now?
}
In pseudocode:

1
2
3
4
5
6
7
8
9
make a boolean that defaults to yes

loop through the two containers, checking each value for equality at that index in the respective containers

if that value is not equal, set that boolean to no

end that loop

if the boolean is true, the containers are identical


this would be the simplest way of implementing a comparison function. There are many ways you can optimize it, but worry about that when you've made the initial algorithm.

Be careful about asking school questions, specific questions will garner specific answers, we need to balance giving you a headstart and letting you learn. There are folks on here who will give you the answers to your questions, just remember you're learning to program, not learning to ask programmers questions.
Last point noted. I'm still not getting the desired output though. I've made some progress as seen in the code below but when it gets to the condition, it skips straight to the else even if equal-length / equal-character strings are input. What gives?

#include <iostream>
#include <string>
#include <deque>
using namespace std;

int main()
{
    deque<char> str1, str2;

    string s;

    cout << "Enter string #1: ";
    cin >> s;

    for (int i = 0; i < s.size(); ++i)
        str1.push_back(s[i]);

    cout << "Enter string #2: ";
    cin >> s;

    for (int i = 0; i < s.size(); ++i)
        str1.push_back(s[i]);

    if (str1.size() == str2.size())
        for (int i = 0; i < str1.size(); ++i)
        {
            if (str1.front() == str2.front())
            {
                str1.pop_front();
                str2.pop_front();
                cout << "Character #" << i + 1 << " is identical." << endl;
            }
            else
                cout << "Strings are not identical." << endl;
        }
    else
        cout << "Strings are not identical." << endl;
}


EDIT: caught the error.
Last edited on
Topic archived. No new replies allowed.