Reverse every word of the string except the first and the last character

Given a string str consisting of a sentence, the task is to reverse every word of the sentence except the first and last character of the words.

Examples:

Input: str = “geeks for geeks”
Output: gkees for gkees



Input: str = “this is a string”
Output: tihs is a snirtg

Approach: Break the string into words using strtok(), now for every word take two pointers i and j pointing to the second and the second last character of the string respectively. Swap these characters then increment i and decrement j. Repeat these steps while i < j.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse the given word except
// the first and the last character
string reverseWord(string str)
{
    int len = str.length();
  
    // Pointer to the second character
    // of the string
    int i = 1;
  
    // Pointer to the second last
    // character of the string
    int j = str.length() - 2;
    while (i < j) {
  
        // Swap str[i] and str[j]
        char temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++;
        j--;
    }
  
    return str;
}
  
// Function to reverse every word of the
// sentence except the first and the
// last character of the words
void reverseWords(char str[])
{
    char* tok = strtok(str, " ");
  
    // While there are words left
    while (tok != NULL) {
  
        // Print the reversed word
        cout << reverseWord(tok) << " ";
  
        // Get the next word
        tok = strtok(NULL, " ");
    }
}
  
// Driver code
int main()
{
    char str[] = "geeks for geeks";
    reverseWords(str);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach 
class GFG 
{
      
// Function to reverse the given word except 
// the first and the last character 
static String reverseWord(String str) 
    int len = str.length(); 
      
    // Pointer to the second character 
    // of the string 
    int i = 1
  
    // Pointer to the second last 
    // character of the string 
    int j = str.length() - 2
      
    char[] strchar = str.toCharArray();
  
    while (i < j)
    
  
        // Swap str[i] and str[j] 
        char temp = strchar[i]; 
        strchar[i] = strchar[j]; 
        strchar[j] = temp; 
        i++; 
        j--; 
    
      
    str = new String(strchar);
    return str; 
  
// Function to reverse every word of the 
// sentence except the first and the 
// last character of the words 
static void reverseWords(String str) 
    String[] tok = str.split("\\s"); 
  
    // While there are words left 
    for(String w:tok) 
    
  
        // Print the reversed word 
        System.out.print(reverseWord(w) + " "); 
    
  
// Driver code 
public static void main (String[] args) 
    String str = "geeks for geeks"
    reverseWords(str); 
}
      
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to reverse the given word except
# the first and the last character
def reverseWord(Str):
      
    # len = len(Str)
  
    # Pointer to the second character
    # of the String
    i = 1
  
    # Pointer to the second last
    # character of the String
    j = len(Str) - 2
    while (i < j):
  
        # Swap Str[i] and Str[j]
        temp = Str[i]
        Str[i] = Str[j]
        Str[j] = temp
        i += 1
        j -= 1
  
    return "".join(Str)
  
# Function to reverse every word of the
# sentence except the first and the
# last character of the words
def reverseWords(Str):
    Str = Str.split()
  
    # While there are words left
    for i in Str:
  
        # Print the reversed word
        j = [h for h in i]
        print(reverseWord(j), end = " ")
  
# Driver code
Str= "geeks for geeks"
reverseWords(Str)
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
      
class GFG 
{
      
// Function to reverse the given word except 
// the first and the last character 
static String reverseWord(String str) 
    int len = str.Length; 
      
    // Pointer to the second character 
    // of the string 
    int i = 1; 
  
    // Pointer to the second last 
    // character of the string 
    int j = str.Length - 2; 
      
    char[] strchar = str.ToCharArray();
  
    while (i < j)
    
  
        // Swap str[i] and str[j] 
        char temp = strchar[i]; 
        strchar[i] = strchar[j]; 
        strchar[j] = temp; 
        i++; 
        j--; 
    
    str = new String(strchar);
    return str; 
  
// Function to reverse every word of the 
// sentence except the first and the 
// last character of the words 
static void reverseWords(String str) 
    String[] tok = str.Split(' '); 
  
    // While there are words left 
    foreach(String w in tok) 
    
  
        // Print the reversed word 
        Console.Write(reverseWord(w) + " "); 
    
  
// Driver code 
public static void Main (String[] args) 
    String str = "geeks for geeks"
    reverseWords(str); 
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

gkees for gkees


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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.