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++
// 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 = 3reverse(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
[tabby title="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 ?> |
geeksrofgeeks
Recommended Posts:
- Sorting array with reverse around middle
- Reverse middle words of a string
- Reverse alternate k characters in a string
- Add index to characters and reverse the string
- Reverse an array without affecting special characters
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum number of operations to move all uppercase characters before all lower case characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Lexicographically middle string
- Middle of three using minimum comparisons
- Shortest Path using Meet In The Middle
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.



