Reverse Middle X Characters

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 [tabby title="PHP"]

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

geeksrofgeeks


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.