LANGUAGE: C++ Write the program exactly the way the instructions are given. Give
ID: 3881941 • Letter: L
Question
LANGUAGE: C++ Write the program exactly the way the instructions are given. Givens: key-decodeSentence.run (OP repository) Description: Design and write a program to process an input line representing a coded message of up to 8 (scrambled) words, and to print out the decoded message with the words in the correct order, followed by ONE SPACE and terminated by two periods (..). NOTE: "word" means a sequence of characters with no spaces. Input format: <position><word> <position><word> ... <position><word> 0-| Example: 1This 4fast. 3is 2cat 0-| ` Output Lines: ENCODED: 1This 4fast. 3is 2cat 0-| DECODED: This cat is fast. .. ENCODED: 0This 5fast. 11is 33cat 0-| DECODED: fast. .. ENCODED: 9This 12fast. 11is 9cat 0-| DECODED: .. DESIGN CONSTRAINTS: (1) Prompt for entire input line: Enter ENCODED Sentence: Note: Input must be the entire line terminated by 0-| (2) Use the data type defined below to arrange the words in order: struct SENTENCE { string word1; string word2; string word3; string word4; string word5; string word6; string word7; string word8; }; (3) Before reading inputs, initialize each word to the empty string. (4) Use a while loop to process the inputs. (5) The loop body must contain a switch based on the word position. (6) If multiple words specify the same position, the earlier word must be replaced by the later one. Example: 4a 2b 1c 4x 2z 0-| ==> c z x .. KEY PROGRAM: in public repository, key-decodeSentence.run Run it to see how your program must behave.
Explanation / Answer
Program for unscramble se
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string string1;
string s1 = " 0-|";
struct SENTENCE
{
string word1;
string word2;
string word3;
string word4;
string word5;
string word6;
string word7;
string word8;
};
SENTENCE sent;
cout<<"Enter an Encoded sentence: ";
getline (cin, string1);
string1 = string1+s1;
cout<<"The Encoded sentence is: "<<string1<<endl;
istringstream iss(string1);
while (iss) {
string word;
iss >> word;
if(sent.word1=="")
{sent.word1=word;
cout << sent.word1 << endl;}
else
if(sent.word2=="")
{sent.word2=word;
cout << sent.word2 << endl;}
else
if(sent.word3=="")
{sent.word3=word;
cout << sent.word3 << endl;}
else
if(sent.word4=="")
{sent.word4=word;
cout << sent.word4 << endl;}
else
if(sent.word5=="")
{sent.word5=word;
cout << sent.word5 << endl;}
else
if(sent.word6=="")
{sent.word6=word;
cout << sent.word6 << endl;}
else
if(sent.word7=="")
{sent.word7=word;
cout << sent.word7 << endl;}
else
if(sent.word8=="")
{sent.word8=word;
cout << sent.word8 << endl;}
}
return 0;
}
Logic for switch can be written like below after words have been stored in in while loop or else we can directly use switch case to save the words in sequence and display to user:
string w1,w2,w3,w4,w5,w6,w7,w8;
while(true)
{
switch(sent.word1.at(0)){
case '1':{
w1=sent.word1;
sent.word1=w1.substr(1);
cout<<w1.substr(1)<<endl;
if(sent.word2.at(0)=='2'){
sent.word2=sent.word2.substr(1);
cout<<sent.word2.substr(1)<<endl;
}
cout<<"The decoded string is: "<<sent.word1+" "+sent.word2;
break;
}
case '2':{
w2=sent.word1;
cout<<w2.substr(1)<<endl;
break;
}
case '3':{
w3=sent.word1;
cout<<w3.substr(1);
break;
}
case '4':{
w4=sent.word1;
cout<<w4.substr(1);
break;
}
case '5':{
w5=sent.word1;
cout<<w5.substr(1);
break;
}
case '6':{
w6=sent.word1;
cout<<w6.substr(1);
break;
}
case '7':{
w7=sent.word1;
cout<<w7.substr(1);
break;
}
case '8':{
w8=sent.word1;
cout<<w8.substr(1);
break;
}
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.