Skip to content
Related Articles

Related Articles

Reverse words in a given string

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 05 Aug, 2022

Let the input string be “i like this program very much”. The function should change the string to “much very program this like i”

reverse-words

Examples

Input: s = “geeks quiz practice code” 
Output: s = “code practice quiz geeks”

Input: s = “getting good at coding needs a lot of practice” 
Output: s = “practice of lot a needs coding at good getting” 

Algorithm:  

  • Initially, reverse the individual words of the given string one by one, for the above example, after reversing individual words the string should be “i ekil siht margorp yrev hcum”.
  • Reverse the whole string from start to end to get the desired output “much very program this like i” in the above example.

Below is the implementation of the above approach: 

C++




// C++ program to reverse a string
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse words*/
void reverseWords(string s)
{
     
    // temporary vector to store all words
    vector<string> tmp;
    string str = "";
    for (int i = 0; i < s.length(); i++)
    {
         
        // Check if we encounter space
        // push word(str) to vector
        // and make str NULL
        if (s[i] == ' ')
        {
            tmp.push_back(str);
            str = "";
        }
 
        // Else add character to
        // str to form current word
        else
            str += s[i];
    }
   
    // Last word remaining,add it to vector
    tmp.push_back(str);
 
    // Now print from last to first in vector
    int i;
    for (i = tmp.size() - 1; i > 0; i--)
        cout << tmp[i] << " ";
    // Last word remaining,print it
    cout << tmp[0] << endl;
}
 
// Driver Code
int main()
{
    string s = "i like this program very much";
    reverseWords(s);
    return 0;
}

C




// C program to reverse a string
#include <stdio.h>
 
// Function to reverse any sequence
// starting with pointer begin and
// ending with pointer end
void reverse(char* begin, char* end)
{
    char temp;
    while (begin < end) {
        temp = *begin;
        *begin++ = *end;
        *end-- = temp;
    }
}
 
// Function to reverse words*/
void reverseWords(char* s)
{
    char* word_begin = s;
 
    // Word boundary
    char* temp = s;
 
    // Reversing individual words as
    // explained in the first step
    while (*temp) {
        temp++;
        if (*temp == '\0') {
            reverse(word_begin, temp - 1);
        }
        else if (*temp == ' ') {
            reverse(word_begin, temp - 1);
            word_begin = temp + 1;
        }
    }
 
    // Reverse the entire string
    reverse(s, temp - 1);
}
 
// Driver Code
int main()
{
    char s[] = "i like this program very much";
    char* temp = s;
    reverseWords(s);
    printf("%s", s);
    return 0;
}

Java




// Java program to
// reverse a String
import java.util.*;
class GFG{
 
// Reverse the letters
// of the word
static void reverse(char str[],
                    int start,
                    int end)
{
  // Temporary variable
  // to store character
  char temp;
   
  while (start <= end)
  {
    // Swapping the first
    // and last character
    temp = str[start];
    str[start] = str[end];
    str[end] = temp;
    start++;
    end--;
  }
}
// Function to reverse words
static char[] reverseWords(char []s)
{
  // Reversing individual words as
  // explained in the first step
 
  int start = 0;
  for (int end = 0; end < s.length; end++)
  {
    // If we see a space, we
    // reverse the previous
    // word (word between
    // the indexes start and end-1
    // i.e., s[start..end-1]
    if (s[end] == ' ')
    {
      reverse(s, start, end);
      start = end + 1;
    }
  }
 
  // Reverse the last word
  reverse(s, start, s.length - 1);
 
  // Reverse the entire String
  reverse(s, 0, s.length - 1);
  return s;
}
 
// Driver Code
public static void main(String[] args)
{
  String s = "i like this program very much ";
  char []p = reverseWords(s.toCharArray());
  System.out.print(p);
}
}
 
// This code is contributed by gauravrajput1

Python3




# Python3 program to reverse a string
 
# Function to reverse each word in the string
def reverse_word(s, start, end):
    while start < end:
        s[start], s[end] = s[end], s[start]
        start = start + 1
        end -= 1
 
 
s = "i like this program very much"
 
# Convert string to list to use it as a char array
s = list(s)
start = 0
while True:
     
    # We use a try catch block because for
    # the last word the list.index() function
    # returns a ValueError as it cannot find
    # a space in the list
    try:
        # Find the next space
        end = s.index(' ', start)
 
        # Call reverse_word function
        # to reverse each word
        reverse_word(s, start, end - 1)
 
        #Update start variable
        start = end + 1
 
    except ValueError:
 
        # Reverse the last word
        reverse_word(s, start, len(s) - 1)
        break
 
# Reverse the entire list
s.reverse()
 
# Convert the list back to
# string using string.join() function
s = "".join(s)
 
print(s)
 
# Solution contributed by Prem Nagdeo

C#




// C# program to
// reverse a String
using System;
 
class GFG
{
 
// Reverse the letters
// of the word
static void reverse(char []str,
                    int start,
                    int end)
{
   
  // Temporary variable
  // to store character
  char temp;
   
  while (start <= end)
  {
     
    // Swapping the first
    // and last character
    temp = str[start];
    str[start] = str[end];
    str[end] = temp;
    start++;
    end--;
  }
}
   
// Function to reverse words
static char[] reverseWords(char []s)
{
   
  // Reversing individual words as
  // explained in the first step
 
  int start = 0;
  for (int end = 0; end < s.Length; end++)
  {
     
    // If we see a space, we
    // reverse the previous
    // word (word between
    // the indexes start and end-1
    // i.e., s[start..end-1]
    if (s[end] == ' ')
    {
      reverse(s, start, end);
      start = end + 1;
    }
  }
 
  // Reverse the last word
  reverse(s, start, s.Length - 1);
 
  // Reverse the entire String
  reverse(s, 0, s.Length - 1);
  return s;
}
 
// Driver Code
public static void Main(String[] args)
{
  String s = "i like this program very much ";
  char []p = reverseWords(s.ToCharArray());
  Console.Write(p);
}
}
 
// This code is contributed by jana_sayantan

Javascript




<script>
    // Javascript program to
    // reverse a String
     
    // Reverse the letters
    // of the word
    function reverse(str,start,end)
    {
        // Temporary variable
        // to store character
        let temp;
         
         
        while (start <= end)
        {
            // Swapping the first
            // and last character
            temp = str[start];
            str[start]=str[end];
            str[end]=temp;
            start++;
            end--;
        }
    }
    // Function to reverse words
    function reverseWords(s)
    {
        // Reversing individual words as
        // explained in the first step
        s=s.split("");
        let start = 0;
        for (let end = 0; end < s.length; end++)
        {
            // If we see a space, we
            // reverse the previous
            // word (word between
            // the indexes start and end-1
            // i.e., s[start..end-1]
            if (s[end] == ' ')
            {
                reverse(s, start, end);
                start = end + 1;
            }
        }
        // Reverse the last word
        reverse(s, start, s.length - 1);
         
        // Reverse the entire String
        reverse(s, 0, s.length - 1);
        return s.join("");
    }
    // Driver Code
    var s = "i like this program very much ";
     
     
    document.write(reverseWords(s));
     
    // This code is contributed by avanitrachhadiya2155
</script>

Output

much very program this like i

Time complexity: O(N)
Auxiliary Space: O(N)

The above code doesn’t handle the cases when the string starts with space. The following version handles this specific case and doesn’t make unnecessary calls to reverse function in the case of multiple spaces in between. Thanks to rka143 for providing this version. 

C




// C program to reverse a string
#include <stdio.h>
 
// Function to reverse any sequence
// starting with pointer begin and
// ending with pointer end
void reverse(char* begin, char* end)
{
    char temp;
    while (begin < end) {
        temp = *begin;
        *begin++ = *end;
        *end-- = temp;
    }
}
 
// C program for above approach
 
void reverseWords(char* s)
{
    char* word_begin = NULL;
   
    //  /* temp is for word boundary */
    char* temp = s;
 
    /*STEP 1 of the above algorithm */
    while (*temp)
    {
 
        /*This condition is to make sure that the
          string start with valid character (not
          space) only*/
        if ((word_begin == NULL) &&
                  (*temp != ' '))
        {
            word_begin = temp;
        }
        if (word_begin && ((*(temp + 1) == ' ') ||
            (*(temp + 1) == '\0')))
        {
            reverse(word_begin, temp);
            word_begin = NULL;
        }
        temp++;
    } /* End of while */
 
    /*STEP 2 of the above algorithm */
    reverse(s, temp - 1);
}
 
// Driver Code
int main()
{
    char s[] = "i like this program very much";
    char* temp = s;
    reverseWords(s);
    printf("%s", s);
    return 0;
}

Output

much very program this like i

Time Complexity: O(N)
Auxiliary Space: O(N) 

Another Approach:

We can do the above task by splitting and saving the string in a reverse manner. 

Below is the implementation of the above approach:

C++




// C++ program to reverse a string
// s = input()
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string s[] = { "i",       "like", "this",
                   "program", "very", "much" };
 
    string ans = "";
    for (int i = 5; i >= 0; i--) {
        ans += s[i] + " ";
    }
    cout << ("Reversed String:") << endl;
    cout << (ans.substr(0, ans.length() - 1)) << endl;
 
    return 0;
}

Java




// Java program to reverse a string
// s = input()
public class ReverseWords
{
 
    public static void main(String[] args)
    {
        String s[] = "i like this program very much".
                                        split(" ");
        String ans = "";
        for (int i = s.length - 1; i >= 0; i--)
        {
            ans += s[i] + " ";
        }
        System.out.println("Reversed String:");
        System.out.println(ans.substring(0,
                                  ans.length() - 1));
    }
}

Python3




# Python3 program to reverse a string
# s = input()
s = "i like this program very much"
words = s.split(' ')
string =[]
for word in words:
    string.insert(0, word)
 
print("Reversed String:")
print(" ".join(string))
 
# Solution proposed bu Uttam

C#




// C# program to reverse a string
// s = input()
 
using System;
public class ReverseWords
{
 
    public static void Main()
    {
        string[] s = "i like this program very much".
                                         Split(' ');
        string ans = "";
        for (int i = s.Length - 1; i >= 0; i--)
        {
            ans += s[i] + " ";
        }
        Console.Write("Reversed String:\n");
        Console.Write(ans.Substring(0,
                                    ans.Length - 1));
    }
}

Javascript




<script>
 
// JavaScript program to reverse a string
 
var s= ["i", "like", "this", "program", "very", "much"];
                                         
    var ans ="";
    for (var i = 5; i >= 0; i--)
    {
        ans += s[i] + " ";
    }
    document.write("Reversed String:"+ "<br>");
    document.write(ans.slice(0,ans.length-1));
 
 
</script>

Output

Reversed String:
much very program this like i

Time Complexity: O(N)
Auxiliary Space: O(N) 

Without using any 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.   

Below is the implementation of the above approach:

C++




// C++ code to reverse a string
 
#include <bits/stdc++.h>
using namespace std;
 
// Reverse the string
string RevString(string s[], int l)
{
 
    // Check if number of words is even
    if (l % 2 == 0) {
 
        // Find the middle word
        int j = l / 2;
 
        // Starting from the middle
        // start swapping words at
        // jth position and l-1-j position
        while (j <= l - 1) {
            string temp;
            temp = s[l - j - 1];
            s[l - j - 1] = s[j];
            s[j] = temp;
            j += 1;
        }
    }
 
    // Check if number of words is odd
    else {
 
        // Find the middle word
        int j = (l / 2) + 1;
 
        // Starting from the middle start
        // swapping the words at jth
        // position and l-1-j position
        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];
 
    // Return the reversed sentence
    for (int i = 1; i < 9; i++) {
        S = S + " " + s[i];
    }
    return S;
}
 
// Driver code
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;
}

Java




// Java code to reverse a string
class GFG{
     
// Reverse the string
public static String[] RevString(String[] s,
                                 int l)
{
     
    // Check if number of words is even
    if (l % 2 == 0)
    {
         
        // Find the middle word
        int j = l / 2;
         
        // Starting from the middle
        // start swapping words at
        // jth position and l-1-j position
        while (j <= l - 1)
        {
            String temp;
            temp = s[l - j - 1];
            s[l - j - 1] = s[j];
            s[j] = temp;
            j += 1;
        }
    }
     
    // Check if number of words is odd
    else
    {
         
        // Find the middle word
        int j = (l / 2) + 1;
         
        // Starting from the middle start
        // swapping the words at jth
        // position and l-1-j position
        while (j <= l - 1)
        {
            String temp;
            temp = s[l - j - 1];
            s[l - j - 1] = s[j];
            s[j] = temp;
            j += 1;
        }
    }
     
    // Return the reversed sentence
    return s;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "getting good at coding " +
               "needs a lot of practice";
    String[] words = s.split("\\s");
     
    words = RevString(words, words.length);
     
    s = String.join(" ", words);
     
    System.out.println(s);
}
}
 
// This code is contributed by MuskanKalra1

Python3




# Python3 code to reverse a string
 
# Reverse the string
def RevString(s,l):
   
  # Check if number of words is even
  if l%2 == 0:
     
    # Find the middle word
    j = int(l/2)
     
    # Starting from the middle
    # start swapping words
    # at jth position and l-1-j position
    while(j <= l - 1):
      s[j], s[l - j - 1] = s[l - j - 1], s[j]
      j += 1
       
  # Check if number of words is odd
  else:
     
    # Find the middle word
    j = int(l/2 + 1)
     
    # Starting from the middle
    # start swapping the words
    # at jth position and l-1-j position
    while(j <= l - 1):
      s[j], s[l - 1 - j] = s[l - j - 1], s[j]
      j += 1
     
    # return the reversed sentence
    return s;
       
# Driver Code
s = 'getting good at coding needs a lot of practice'
string = s.split(' ')
string = RevString(string,len(string))
print(" ".join(string))

C#




// C# code to reverse a string
using System;
class GFG{
     
// Reverse the string
static string RevString(string[] s, int l)
{
     
    // Check if number of words is even
    if (l % 2 == 0)
    {
         
        // Find the middle word
        int j = l / 2;
         
        // Starting from the middle
        // start swapping words at
        // jth position and l-1-j position
        while (j <= l - 1)
        {
            string temp;
            temp = s[l - j - 1];
            s[l - j - 1] = s[j];
            s[j] = temp;
            j += 1;
        }
    }
     
    // Check if number of words is odd
    else
    {
         
        // Find the middle word
        int j = (l / 2) + 1;
         
        // Starting from the middle start
        // swapping the words at jth
        // position and l-1-j position
        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];
  
    // Return the reversed sentence
    for (int i = 1; i < 9; i++) {
        S = S + " " + s[i];
    }
    return S;
}
 
// Driver Code
public static void Main()
{
    string []words
        = { "getting", "good", "at", "coding""needs",
            "a",       "lot""of", "practice" };
     
    string a = RevString(words, words.Length);
     
    Console.WriteLine(a);
}
}
 
// This code is contributed by Aarti_Rathi and shivanisinghss2110

Javascript




<script>
 
// Javascript code to reverse a string
 
// Reverse the string
function RevString(s, l)
{
     
    // Check if number of words is even
    if (l % 2 == 0)
    {
 
        // Find the middle word
        let j = parseInt(l / 2, 10);
 
        // Starting from the middle
        // start swapping words at
        // jth position and l-1-j position
        while (j <= l - 1)
        {
            let temp;
            temp = s[l - j - 1];
            s[l - j - 1] = s[j];
            s[j] = temp;
            j += 1;
        }
    }
 
    // Check if number of words is odd
    else
    {
 
        // Find the middle word
        let j = parseInt((l / 2), 10) + 1;
 
        // Starting from the middle start
        // swapping the words at jth
        // position and l-1-j position
        while (j <= l - 1)
        {
            let temp;
            temp = s[l - j - 1];
            s[l - j - 1] = s[j];
            s[j] = temp;
            j += 1;
        }
    }
 
    let S = s[0];
 
    // Return the reversed sentence
    for(let i = 1; i < 9; i++)
    {
        S = S + " " + s[i];
    }
    return S;
}
 
// Driver code
let s = "getting good at coding "
        "needs a lot of practice";
             
let words = [ "getting", "good", "at",
              "coding", "needs", "a",
              "lot", "of", "practice"];
   
document.write(RevString(words, 9));
 
// This code is contributed by suresh07
     
</script>

Output

practice of lot a needs coding at good getting

Time complexity: O(N)
Auxiliary Space: O(N)

Another intuitive constant space solution

Go through the string and mirror each word in the string, then, in the end, mirror the whole string.

The following C++ code can handle multiple contiguous spaces.

C++




#include <algorithm>
#include <iostream>
#include <string>
 
using namespace std;
 
string reverse_words(string s)
{
    int left = 0, i = 0, n = s.size();
 
    while (s[i] == ' ')
        i++;
 
    left = i;
 
    while (i < n) {
        if (i + 1 == n || s[i] == ' ') {
            int j = i - 1;
            if (i + 1 == n)
                j++;
 
            while (left < j)
                swap(s[left++], s[j--]);
 
            left = i + 1;
        }
        if (i > left && s[left] == ' ')
            left = i;
 
        i++;
    }
    reverse(s.begin(), s.end());
    return s;
}
 
int main()
{
 
    string str = "Be a game changer the world is already "
                 "full of players";
 
    str = reverse_words(str);
 
    cout << str;
 
    return 0;
}

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    // Java does not have built-in swap function to swap
    // characters of a string
    public 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 new String(ch);
    }
 
    public static String reverse_words(String s)
    {
        int left = 0, i = 0, n = s.length();
 
        while (s.charAt(i) == ' ')
            i++;
 
        left = i;
 
        while (i < n) {
            if (i + 1 == n || s.charAt(i) == ' ') {
                int j = i - 1;
                if (i + 1 == n)
                    j++;
 
                while (left < j)
                    s = swap(s, left++, j--);
 
                left = i + 1;
            }
            if (i > left && s.charAt(left) == ' ')
                left = i;
 
            i++;
        }
        // Use StringBuilder ".reverse()" method to reverse
        // the whole string.
        s = new StringBuilder(s).reverse().toString();
        return s;
    }
    // Driver Code
    public static void main(String[] args)
    {
        String str
            = "Be a game changer the world is already full of players";
        str = reverse_words(str);
        System.out.println(str);
    }
}
//This code is contributed by KaaL-EL.

Output

players of full already is world the changer game a Be

Time complexity: O(N)
Auxiliary Space: O(1)

Method:  Using slicing method and join functions.

Python3




# python code to reverse words in a given string
 
# input string
string = "getting good at coding needs a lot of practice"
 
# spliting words in the given string
# using slicing reverse the words
s = string.split()[::-1]
 
# joining the reversed string and
# printing the output
print(" ".join(s))

PHP




<?php
$string = "getting good at coding needs a lot of practice";
$array = explode(" ", $string);
$rarray = array_reverse($array);
  
echo $newstring = implode(" ", $rarray);
  
// this code is contributed by gangarajula laxmi
?>

C#




using System;
using System.Linq;
public class GFG
{
  static public void Main ()
  {
    string text = "getting good at coding needs a lot of practice";
    Console.WriteLine(string.Join(" ", text.Split(' ').Reverse()));
  }
}
 
// this code is contributed by gangarajula laxmi

Output

practice of lot a needs coding at good getting

Time complexity: O(N2)
Auxiliary Space: O(N)

Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!