Part 2: Read a sentence, word by word, until the user types a period Write a pro
ID: 3870609 • Letter: P
Question
Part 2: Read a sentence, word by word, until the user types a period Write a program that reads words from the console until the user enters a period. Then output the sentence. For example Enter a word, period to end: The Enter a word, period to end: Enter a word, period to end: brown Enter a word, period to Enter a word, period to end: . quick end: fox Your sentence is he quick brown fox . Hint: string variables can be "added" with . Also, strings can be compared against other strings or against string constants. For example strings! "hello", s2 "world", s3; coutExplanation / Answer
#include<iostream>
using namespace std;
int main() {
string s, sentence="";
cout<<"Enter a word, period to end: ";
cin >> s;
while(s !=".") {
sentence = sentence+s+" ";
cout<<"Enter a word, period to end: ";
cin >> s;
}
cout<<"The sentence is: "<<endl;
cout<<sentence<<endl;
return 0;
}
Output:
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.