Skip to content
Related Articles

Related Articles

Reverse Middle X Characters
  • Last Updated : 07 Apr, 2021

Given a string str and an integer X. The task is to reverse the middle X characters of the given string and then print the modified string. Note that len(str) – X is always even.
Examples: 
 

Input: str = “geeksforgeeks”, X = 3 
Output: geeksrofgeeks 
Middle three character are “geeksforgeeks” 
Hence the resultant string is “geeksrofgeeks”
Input: str = “acknowledgement”, X = 7 
Output: acknegdelwoment 
 

 

Approach: 
 

  • Since we need not reverse the first and last few character. Find the number of characters that we need not reverse in the beginning and in the end i.e. n = len(str) – X / 2.
  • Print the first n characters as it is.
  • Then print the middle x characters starting from n + 1 in reverse order.
  • Finally, the last n characters as it is.

Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function to reverse the middle x characters in a string
void reverse(string str, int x)
{
    // Find the position from where
    // the characters have to be reversed
    int n = (str.length() - x) / 2;
 
    // Print the first n characters
    for (int i = 0; i < n; i++)
        cout << str[i];
 
    // Print the middle x characters in reverse
    for (int i = n + x - 1; i >= n; i--)
        cout << str[i];
 
    // Print the last n characters
    for (int i = n + x; i < str.length(); i++)
        cout << str[i];
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int x = 3;
    reverse(str, x);
    return 0;
}

Java




// Java implementation of the above approach
class GfG
{
 
    // Function to reverse the middle x
    // characters in a string
    static void reverse(String str, int x)
    {
        // Find the position from where
        // the characters have to be reversed
        int n = (str.length() - x) / 2;
     
        // Print the first n characters
        for (int i = 0; i < n; i++)
            System.out.print(str.charAt(i));
     
        // Print the middle x characters in reverse
        for (int i = n + x - 1; i >= n; i--)
            System.out.print(str.charAt(i));
     
        // Print the last n characters
        for (int i = n + x; i < str.length(); i++)
            System.out.print(str.charAt(i));
    }
 
    // Drived code
    public static void main(String []args)
    {
        String str = "geeksforgeeks";
        int x = 3;
        reverse(str, x);
    }
}
 
// This code is contributed by Rituraj Jain

Python3




# Python3 implementation of the approach
 
# Function to reverse the middle x characters in a str1ing
def reverse(str1, x):
 
    # Find the position from where
    # the characters have to be reversed
    n = (len(str1) - x) // 2
 
    # Print the first n characters
    for i in range(n):
        print(str1[i], end="")
 
    # Print the middle x characters in reverse
    for i in range(n + x - 1, n - 1, -1):
        print(str1[i], end="")
 
    # Print the last n characters
    for i in range(n + x, len(str1)):
        print(str1[i], end="")
 
 
# Driver code
str1 = "geeksforgeeks"
x = 3
reverse(str1, x)
 
# This code is contributed by mohit kumar 29.

C#




// C# implementation of the above approach
using System;
 
class GFG
{
 
// Function to reverse the middle x
// characters in a string
static void reverse(string str, int x)
{
    // Find the position from where
    // the characters have to be reversed
    int n = (str.Length - x) / 2;
 
    // Print the first n characters
    for (int i = 0; i < n; i++)
        Console.Write(str[i]);
 
    // Print the middle x characters in reverse
    for (int i = n + x - 1; i >= n; i--)
        Console.Write(str[i]);
 
    // Print the last n characters
    for (int i = n + x; i < str.Length; i++)
        Console.Write(str[i]);
}
 
// Drived code
public static void Main()
{
    string str = "geeksforgeeks";
    int x = 3;
    reverse(str, x);
}
}
 
// This code is contributed
// by Akanksha Rai

PHP




<?php
// PHP implementation of the approach
 
// Function to reverse the middle x
// characters in a string
function reverse($str, $x)
{
    // Find the position from where
    // the characters have to be reversed
    $n = (strlen($str) - $x) / 2;
 
    // Print the first n characters
    for ($i = 0; $i < $n; $i++)
        echo($str[$i]);
 
    // Print the middle x characters in reverse
    for ($i = $n + $x - 1; $i >= $n; $i--)
        echo($str[$i]);
 
    // Print the last n characters
    for ($i= $n + $x; $i < strlen($str); $i++)
        echo $str[$i];
}
 
// Driver code
$str = "geeksforgeeks";
$x = 3;
reverse($str, $x);
 
// This code is contributed by Shivi_Aggarwal
?>

Javascript




<script>
 
// JavaScript implementation of the above approach
 
    // Function to reverse the middle x
    // characters in a string
    function reverse( str , x)
    {
        // Find the position from where
        // the characters have to be reversed
        var n = (str.length - x) / 2;
 
        // Print the first n characters
        for (i = 0; i < n; i++)
            document.write(str.charAt(i));
 
        // Print the middle x characters in reverse
        for (i = n + x - 1; i >= n; i--)
            document.write(str.charAt(i));
 
        // Print the last n characters
        for (i = n + x; i < str.length; i++)
            document.write(str.charAt(i));
    }
 
    // Drived code
     
        var str = "geeksforgeeks";
        var x = 3;
        reverse(str, x);
 
// This code contributed by aashish1995
 
</script>
Output: 
geeksrofgeeks

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :