Write a program that reads two words from a file. The first word is processed an
ID: 3806390 • Letter: W
Question
Write a program that reads two words from a file. The first word is processed and stored as a ‘stack’ of characters (first letter of word will be at the bottom of the stack). The second word is processed and stored as a ‘queue’ of characters. Using stack and queue operations determine if the words are the same? Note - Be sure to include ‘*****StackType.h’ and ‘******QueueType.h’ in your .cpp.
Sample:
Input file: BOWIE BULLDOGS Stack has
E <- top
I
W
O
B
Queue has
B U L L D O G S
Front Back
Output for data above: Words are NOT the same
...oo Verizon 32% | ... .. .. .. ... a il a li li l i l uilil lli l i l i lilii| ...ii i ii i li li!Explanation / Answer
Please refer below code
#include<iostream>
#include<stack>
#include<queue>
#include<fstream>
using namespace std;
int main()
{
std::stack<char> st;
std::queue<char> Q;
char ch;
int flag = 1;
string word1,word2;
std::ifstream myfile("input.txt");
myfile>>word1>>word2;
for(unsigned int i = 0; i < word1.length(); i++)
{
ch = word1[i];
st.push(ch);
//cout<<ch<<endl;
}
for(unsigned int i = 0; i < word2.length(); i++)
{
ch = word2[i];
Q.push(ch);
}
cout<<Q.back()<<endl;
if(st.size() != Q.size())
{
cout<<"Words are NOT same"<<endl;
}
else
{
while(!st.empty())
{
if(st.top() != Q.back())
{
flag = 0;
break;
}
st.pop();
}
if(!flag)
cout<<"Words are same"<<endl;
else
cout<<"Words are NOT same"<<endl;
}
return 0;
}
input.txt is
BOWIE BULLDOGS
Please refer below output
Words are NOT same
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.