Question: #include #include #include #include #include std::string reverseWords ( const std::string& input ) { std::istringstream iss ( input ) ; std::vector words { std::istream _ iterator
#include
#include
#include
#include
#include
std::string reverseWordsconst std::string& input
std::istringstream issinput;
std::vector words
std::istreamiteratoriss
std::istreamiterator
;
std::reversewordsbegin words.end;
std::ostringstream oss;
for const auto& word : words
oss word ;
if wordback
oss ; Add space after the comma
std::string result oss.str;
if result.empty
result.popback; Remove the trailing space
return result;
int main
std::string input Do or do not, there is no try";
std::string reversed reverseWordsinput;
std::cout "Original: input std::endl;
std::cout "Reversed: reversed std::endl;
return ;
The code above has a function that that outputs:
Original: Do or do not, there is no try
Reversed: try no is there not, do or Do
I want the reversed portion of the output to have a comma between the words "there" and "not" so that the output can look like this instead:
Original: Do or do not, there is no try
Reversed: try no is there, not do or Do
How can I edit my code so that the comma can be placed in the intended position?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
