In: Computer Science
In this Exercise, you have to take a single string as input.
Using this input string, you have to create multiple
queues in which each queue will comprise of separate word appeared
in input string. At the end, you will again
concatenate all queues to a single queue.s
Example:
String = “Data Structure and
Algo”
Q1 = D → a → t → a
Q2 = S → t → r → u → c → t → u → r→e
Q3 = a → n → d
Q4 = A → l → g → o
At the end concatenate all queues and display them.
Q1 → Q2 → Q3 → Q4
c++ language
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
//taking the complete line as input
getline(cin, s);
int k = 1;
bool flag = false;
//printing each queue by splitting at every space
for(int i = 0; i<s.length(); i++){
if(s[i] == ' '){
k++;
flag = false;
}else{
//generating queue template Q[i]
if(!flag){
cout << endl;
cout << "Q" << k << " = ";
flag = true;
}
cout << s[i];
if((i+1 < s.length()) && (s[i+1] != ' ')){
cout << " -> ";
}
}
}
//concatinating all the queue
cout << "\nQ = ";
for(int i = 0; i < s.length(); i++){
if(s[i] == ' '){
continue;
}
cout << s[i] ;
if(i == s.length()-1){
continue;
}
cout << " -> ";
}
return 0;
}
ouput will look like this
I have tried my best to make it more understandable. Despite of this if you have any doubt, comment below i would be more then happy to help you out.