Question
C++
4. Write a loop that reads in a collection of words and builds a sentence out of all the words by appending each new word to the string being formed. For example, if the three words This, is, one are entered, your sentence would be This, then This is, and finally This is one. Exit your loop when a sentinel word is entered.
media%2Fdd0%2Fdd010fce-f28e-4d41-b0c4-45
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<vector>

#include<string>

#include<algorithm>

#include<iostream>

using namespace std;

int main(){

vector<string> words;

cout<<"Please enter quit to stop"<<endl;

string temp;

while(cin>>temp){ //input word

transform(temp.begin(), temp.end(), temp.begin(), ::tolower); //convert word to lower case

if(temp == "quit"){

break;

}

words.push_back(temp); //insert word in the vector

}

for (int i=0; i < words.size(); i++) { //display word in sentences

for (int j=0; j<=i; j++) {

cout <<words[j]<<" ";

}

cout<<endl;

}

}

PROGRAM

1 2 3 #înclude«vector» #include<string> #include«algorithm» 5 #include<iostream> 6 using namespace std; 8 int main(){ vector<string words; cout<<Please enter quit to stop<<endl; 10 12 13 14 15 16 17 18 19 20 21 string temp; while(cin>>temp){ //input word transform (temp.begin(), temp.end), temp.begin(), ::tolower) //convert word to Lower case if(temp - quit){ break; //insert word in the vector words.push_back (temp) //display word in sentences for (int i-0; i < words.size(); i++) f for (int j=0; j(#1; j++) { 23 24 25 26 27 28 L cout <<words[j]<< ; cout<<endl;OUTPUT

Add a comment
Know the answer?
Add Answer to:
C++ 4. Write a loop that reads in a collection of words and builds a sentence...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

    String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak....

  • Task 1: String with Loop • Write a program that reads a sentence and use a...

    Task 1: String with Loop • Write a program that reads a sentence and use a loop that counts how many elements in a string that are equal to ‘t’. Task 2: List with Function • Write a function fill that fills all elements of a list with a given value. • For example: o The call fill(scores, 13) should fill all elements of the list scores with the value 13. • Write a demo program to show how this...

  • JAVA PROGRAMMING LANGUAGE!!!! Pig Latin Write a program that reads a sentence as input and converts...

    JAVA PROGRAMMING LANGUAGE!!!! Pig Latin Write a program that reads a sentence as input and converts each word to "Pig Latin". In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word. Here is an example: English:                       I SLEPT MOST OF THE NIGHT Pig Latin :                   TAY LEPTSAY OSTMAY FOÀY HETAY 1GHTNAY make sure that there are two...

  • CODE: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as...

    CODE: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the word if it is part of a larger word. For example,...

  • PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """...

    PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the...

  • Write a Java program that reads in 5 words from the user and stores these words...

    Write a Java program that reads in 5 words from the user and stores these words in a String array. Then your program needs to print 2 lines of output with the following information: All words are in reverse order (based both on content and order). Every word at an even index of the array (i.e., indices 0, 2, and 4). For example, if user inputs "ABC", "DEF", "TY", "JK", and "WE". Then your program should output the following two...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • This lab will combine reading data from a file and searching the array to find a...

    This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...

  • 14.3: More Sentences Write a program that allows a user to enter a sentence and then...

    14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...

  • C++ Write a loop that reads positive integers from standard input and that terminates when it...

    C++ Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT