Question

In: Computer Science

In this Exercise, you have to take a single string as input. Using this input string,...

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

Solutions

Expert Solution

#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.


Related Solutions

The assignment is to build a program in Python that can take a string as input...
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.)  For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words.  In the output, each line contains of a...
takes a single string d as input which represents a date
takes a single string d as input which represents a date
Your assignment is to build a program that can take a string as input and produce...
Your assignment is to build a program that can take a string as input and produce a “frequency list” of all of the words in the string (see the definition of a word below.) For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words. In the output, each line contains of...
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input...
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input string will be a sentance ex: "The cat is on the chair and the bear is on the chair") -- returns a table (word varchar(max), count int) (Output is a table that shows how many times each word appeared in the sentance) Where each space-delimited “word” in the string appears in the table along with the number of times it appeared in the input...
In this exercise, you will be given a system with their input/output relationships. Using MATLAB, determine...
In this exercise, you will be given a system with their input/output relationships. Using MATLAB, determine whether the system below are a) linear/non-linear b) time-invariant/timevariant, c) causal/noncausal, d) has memory/memoryless: y[n] = x2[n] Provide MATLAB code and graphs to show your work for the linearity and time-invariance testing
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT