Program to reverse words in a given string in C++
Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++.
Examples:
Input: str = “the sky is blue”
Output: blue is sky the
Input: str = “I love programming”
Output: programming love I
Method 1: Using STL functions
- Reverse the given string str using STL function reverse().
- Iterate the reversed string and whenever a space is found reverse the word before that space using the STL function reverse().
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
string reverseString(string str)
{
reverse(str.begin(), str.end());
str.insert(str.end(), ' ' );
int n = str.length();
int j = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == ' ' ) {
reverse(str.begin() + j, str.begin() + i);
j = i + 1;
}
}
str.pop_back();
return str;
}
int main()
{
string str = "I like this code" ;
string rev = reverseString(str);
cout << rev;
return 0;
}
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Without using the inbuilt function: We can create the reverse() function which is used to reverse the given string. Below are the steps:
- Initially reverse each word of the given string str.
- Now reverse the whole string to get the resultant string in desired order.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void reversed(string& s, int l, int r)
{
while (l < r) {
swap(s[l], s[r]);
l++;
r--;
}
}
string reverseString(string str)
{
str.insert(str.end(), ' ' );
int n = str.length();
int j = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == ' ' ) {
reversed(str, j, i - 1);
j = i + 1;
}
}
str.pop_back();
reversed(str, 0, str.length() - 1);
return str;
}
int main()
{
string str = "I like this code" ;
string rev = reverseString(str);
cout << rev;
return 0;
}
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3: Without Using Extra Space
The above task can also be accomplished by splitting and directly swapping the string starting from the middle. As direct swapping is involved, less space is consumed too.
C++
#include <bits/stdc++.h>
using namespace std;
string RevString(string s[], int l)
{
if (l % 2 == 0) {
int j = l / 2;
while (j <= l - 1) {
string temp;
temp = s[l - j - 1];
s[l - j - 1] = s[j];
s[j] = temp;
j += 1;
}
}
else {
int j = (l / 2) + 1;
while (j <= l - 1) {
string temp;
temp = s[l - j - 1];
s[l - j - 1] = s[j];
s[j] = temp;
j += 1;
}
}
string S = s[0];
for ( int i = 1; i < 9; i++) {
S = S + " " + s[i];
}
return S;
}
int main()
{
string s = "getting good at coding "
"needs a lot of practice" ;
string words[]
= { "getting" , "good" , "at" , "coding" , "needs" ,
"a" , "lot" , "of" , "practice" };
cout << RevString(words, 9) << endl;
return 0;
}
|
Output
practice of lot a needs coding at good getting
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using stringstream and vector
- Create a stringstream from the given string.
- Create an empty vector to store the words of the sentence.
- Read each word from the stringstream and store it in the vector.
- Reverse each word in the vector.
- Concatenate the reversed words with a space between each word.
- Remove the extra space at the end of the concatenated string.
- Reverse the entire concatenated string.
- Return the reversed sentence.
C++
#include <bits/stdc++.h>
using namespace std;
string reverseString(string str)
{
stringstream ss(str);
vector<string> words;
string temp;
while (ss >> temp) {
words.push_back(temp);
}
for ( int i = 0; i < words.size(); i++) {
reverse(words[i].begin(), words[i].end());
}
string rev = "" ;
for ( int i = 0; i < words.size(); i++) {
rev += words[i] + " " ;
}
rev.pop_back();
reverse(rev.begin(), rev.end());
return rev;
}
int main()
{
string str = "I like this code" ;
string rev = reverseString(str);
cout << rev;
return 0;
}
|
Time complexity is O(n)
Auxiliary space is O(n)
Last Updated :
06 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...