Reverse middle words of a string

Given a string str, print reverse all words except the first and last words.

Examples:

Input  : Hi how are you geeks 
Output  : Hi woh era uoy geeks

Input : I am fine
Output : I ma fine


  1. Print the first word.
  2. For remaining middle words, print reverse of every word after reaching end of it. This will print reverse of all words except the last word.
  3. Print last word.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print reverse of all words
// except the corner words.
#include <bits/stdc++.h>
using namespace std;
  
void printReverse(string str)
{
    // Print first word
    int i = 0;
    for (i = 0; i < str.length() && str[i] != ' '; i++)
        cout << str[i];
  
    // Print middle words
    string word = "";
    for (; i < str.length(); i++) {
  
        if (str[i] != ' ')
            word += str[i];
        else {
            reverse(word.begin(), word.end());
            cout << word << " ";
            word = "";
        }
    }
  
    // Print last word
    cout << word << " ";
}
  
int main()
{
    string str = "Hi how are you geeks";
    printReverse(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print reverse of all words 
// except the corner words. 
  
public class GFG{
  
static void printReverse(String str) 
    
        // Print first word 
        int i = 0
        for (i = 0; i < str.length() && str.charAt(i) != ' '; i++) 
            System.out.print(str.charAt(i)) ;
      
        // Print middle words 
        String word = ""
          
          
        for (; i < str.length(); i++) { 
      
            if (str.charAt(i) != ' '
                word += str.charAt(i);
                  
            else
                    System.out.print(new StringBuilder(word).
                                reverse().toString() + " ");
                    word = ""
            
        
      
        // Print last word 
        System.out.print(word + " "); 
    
      
    public static void main(String []args) 
    
        String str = "Hi how are you geeks"
        printReverse(str); 
    
  
// This code is contributed by Ryuga 
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print reverse of all words
# except the corner words.
def printReverse(s):
  
    #Taking all the words in a list
    l = [s for s in s.split(' ')] 
  
    #printing the first word as it is
    print(l[0], end = ' ')
  
      
    for i in range(1, len(l)-1):
  
        #printing middle words reversed
        print(l[i][::-1], end = ' ')
  
    #printing the last word as it is
    print(l[len(l)-1])
  
s = "Hi how are you geeks"
printReverse(s)

chevron_right


PHP

Output:

Hi woh era uoy geeks

Time Complexity : O(n)
Auxiliary Space : O(L) where L is length of the longest word in the string.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Silu Panda, Ryuga, Ita_c