You are given a sentence, reverse the string word by word

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

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *