Given a string str, the task is to reverse all the individual words.
Examples:
Input: str = “Hello World”
Output: olleH dlroW
Input: str = “Geeks for Geeks”
Output: skeeG rof skeeG
Approach: A solution to the above problem has been discussed in this post. It has a time complexity of O(n) and uses O(n) extra space. In this post, we will discuss a solution which uses O(1) extra space.
- Traverse through the string until we encounter a space.
- After encountering the space, we use two variables ‘start’ and ‘end’ pointing to the first and last character of the word and we reverse that particular word.
- Repeat the above steps until the last word.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string reverseWords(string str)
{
int start = 0;
for ( int i = 0; i <= str.size(); i++) {
if (str[i] == ' ' || i == str.size()) {
int end = i - 1;
while (start < end) {
swap(str[start], str[end]);
start++;
end--;
}
start = i + 1;
}
}
return str;
}
int main()
{
string str = "Geeks for Geeks" ;
cout << reverseWords(str);
return 0;
}
|
Java
class GFG
{
static String swap(String str, int i, int j)
{
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(i, str.charAt(j));
sb.setCharAt(j, str.charAt(i));
return sb.toString();
}
static String reverseWords(String str)
{
int start = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == ' ' ||
i == str.length()- 1 )
{
int end;
if (i == str.length()- 1 )
end = i ;
else
end = i - 1 ;
while (start < end)
{
str = swap(str,start,end) ;
start++;
end--;
}
start = i + 1 ;
}
}
return str ;
}
public static void main(String args[])
{
String str = "Geeks for Geeks" ;
System.out.println(reverseWords(str));
}
}
|
Python3
def reverseWords( Str ):
start = 0
for i in range ( len ( Str )):
if ( Str [i] = = ' ' or i = = len ( Str ) - 1 ):
end = i - 1
if (i = = len ( Str ) - 1 ):
end = i
while (start < end):
Str [start], Str [end] = Str [end], Str [start]
start + = 1
end - = 1
start = i + 1
return "".join( Str )
Str = "Geeks for Geeks"
Str = [i for i in Str ]
print (reverseWords( Str ))
|
C#
using System;
class GFG
{
static String reverseWords(String str)
{
int start = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == ' ' ||
i == str.Length-1 )
{
int end;
if (i == str.Length-1)
end = i ;
else
end = i - 1 ;
while (start < end)
{
str = swap(str,start,end) ;
start++;
end--;
}
start = i + 1;
}
}
return str ;
}
static String swap(String str, int i, int j)
{
char []ch = str.ToCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return String.Join( "" ,ch);
}
public static void Main(String []args)
{
String str = "Geeks for Geeks" ;
Console.WriteLine(reverseWords(str));
}
}
|
Javascript
<script>
function swap(str, i, j)
{
let sb = str.split( "" );
sb[i] = str[j];
sb[j] = str[i];
return sb.join( "" );
}
function reverseWords(str)
{
let start = 0;
for (let i = 0; i < str.length; i++)
{
if (str[i] == ' ' ||
i == str.length-1 )
{
let end;
if (i == str.length-1)
end = i ;
else
end = i - 1 ;
while (start < end)
{
str = swap(str, start, end);
start++;
end--;
}
start = i + 1;
}
}
return str;
}
let str = "Geeks for Geeks" ;
document.write(reverseWords(str));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)