Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 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++




// 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;
}

Java




// 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

Python3




# 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

C#




// 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

Javascript




<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>

Output: 

gkees for gkees

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!