ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT

You are given a sentence, reverse the string word by word in C++

prodevelopertutorial March 5, 2019
Example:  
Input: "prodevelopertutorial is a good website",
Output: " website good a is prodevelopertutorial".

The solution is as follows:

Step 1: Reverse the whole string.

Step 2: Reverse individual words in the string.

Along with the above 2 steps, we need to remove leading and trailing spaces, and extra spaces in between the words.

Solution in C++

#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

void reverseWords(string &str) 
{
	int start = 0;
	int i = 0;

	cout<<"\n\n============debugging logs start===========";
	//remove leading space
	while( str[i] == ' ') str.erase(i, 1);
	cout<<"\nStep 1 after removing leading space = "<<str<<endl;

	//reverse words
	reverse(str.begin(), str.end());
	cout<<"Step 2 after reversing the words = "<<str<<endl;

	//remove trailing zeros
	while( str[i] == ' ') str.erase(i, 1);
	cout<<"Step 3 after trailing zeros= "<<str<<endl;

	int size = str.size();

	for(i = 0; i < size - 1; i++)
	{
		//check if you encounter a space.
		// if you encounter a space, then it is a word, reverse the word.
		if( str[i] == ' ')
		{

			if( i == start )
			{
				// if there is more than one space between two words, remove extra space
				str.erase(i--,1);
				size--;
				cout<<"Step 4 removing extra space between words = "<<str<<endl;

			}
			else
			{

				// reverse word.
				reverse(str.begin()+start, str.begin()+i);
				start = i+1;
				cout<<"Step 5 reversing single word = "<<str<<endl;

			}
		}
	}

// reverse the last word.
reverse(str.begin() + start, str.end());
cout<<"Step 6 reversing last word = "<<str<<endl;
cout<<"============debugging logs end===========\n\n"<<endl;


}

int main()
{
	string str = "  Hello    world   from    prodevelopertutorial.com   ";
	cout<<"\nOriginal string = "<<endl;
	cout<<"\""<< str <<"\"";

	reverseWords(str);
	
	cout<<"String after reversing words = "<<endl;
	cout<<str<<endl;

	return 0;

}

Output:

Original string =

"  Hello    world   from    prodevelopertutorial.com   "

============debugging logs start===========

Step 1 after removing leading space = Hello    world   from    prodevelopertutorial.com

Step 2 after reversing the words =    moc.lairotutrepolevedorp    morf   dlrow    olleH

Step 3 after trailing zeros= moc.lairotutrepolevedorp    morf   dlrow    olleH

Step 5 reversing single word = prodevelopertutorial.com    morf   dlrow    olleH

Step 4 removing extra space between words = prodevelopertutorial.com   morf   dlrow    olleH

Step 4 removing extra space between words = prodevelopertutorial.com  morf   dlrow    olleH

Step 4 removing extra space between words = prodevelopertutorial.com morf   dlrow    olleH

Step 5 reversing single word = prodevelopertutorial.com from   dlrow    olleH

Step 4 removing extra space between words = prodevelopertutorial.com from  dlrow    olleH

Step 4 removing extra space between words = prodevelopertutorial.com from dlrow    olleH

Step 5 reversing single word = prodevelopertutorial.com from world    olleH

Step 4 removing extra space between words = prodevelopertutorial.com from world   olleH

Step 4 removing extra space between words = prodevelopertutorial.com from world  olleH

Step 4 removing extra space between words = prodevelopertutorial.com from world olleH

Step 6 reversing last word = prodevelopertutorial.com from world Hello

============debugging logs end===========


String after reversing words =

prodevelopertutorial.com from world Hello

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Daily we discuss about competitive programming questions, join us at:   Telegram Channel

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2021 ProDeveloperTutorial.com
Get top courses from: Educative.io