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

07 Feb, 2022
Given 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: this 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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to reverse the given word except// the first and the last characterstring 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 wordsvoid 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 codeint main(){    char str[] = "geeks for geeks";    reverseWords(str);     return 0;}
// Java implementation of the above approachclass GFG{     // Function to reverse the given word except// the first and the last characterstatic 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 wordsstatic 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 codepublic static void main (String[] args){    String str = "geeks for geeks";    reverseWords(str);}}     // This code is contributed by AnkitRai01
# Python3 implementation of the approach # Function to reverse the given word except# the first and the last characterdef 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 wordsdef 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 codeStr= "geeks for geeks"reverseWords(Str) # This code is contributed by Mohit Kumar
// C# implementation of the above approachusing System;     class GFG{     // Function to reverse the given word except// the first and the last characterstatic 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 wordsstatic 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 codepublic static void Main (String[] args){    String str = "geeks for geeks";    reverseWords(str);}} // This code is contributed by 29AjayKumar
<script>      // JavaScript implementation of the above approach      // Function to reverse the given word except      // the first and the last character      function reverseWord(str) {        var len = str.length;         // Pointer to the second character        // of the string        var i = 1;         // Pointer to the second last        // character of the string        var j = str.length - 2;         var strchar = str.split("");         while (i < j) {          // Swap str[i] and str[j]          var temp = strchar[i];          strchar[i] = strchar[j];          strchar[j] = temp;          i++;          j--;        }        str = strchar.join("");        return str;      }       // Function to reverse every word of the      // sentence except the first and the      // last character of the words      function reverseWords(str) {        var tok = str.split(" ");         // While there are words left        for (const w of tok) {          // Print the reversed word          document.write(reverseWord(w) + " ");        }      }       // Driver code      var str = "geeks for geeks";      reverseWords(str);</script>
gkees for gkees
 
mohit kumar 29
ankthon
29AjayKumar
nidhi_biet
rdtank
gulshankumarar231
Reverse
Strings
Strings
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 String Coding Problems for Interviews
Vigenère Cipher
How to Append a Character to a String in C
Program to add two binary strings
Convert character array to string in C++
Return maximum occurring character in an input string
Naive algorithm for Pattern Searching
Hill Cipher
sprintf() in C
A Program to check if strings are rotations of each other or not