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.
Input: str = “geeksforgeeks”, X = 3
Middle three character are “geeksforgeeks”
Hence the resultant string is “geeksrofgeeks”
Input: str = “acknowledgement”, X = 7
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sorting array with reverse around middle
- Reverse middle words of a string
- Reverse a string without affecting special characters
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Add index to characters and reverse the string
- Reverse alternate k characters in a string
- Middle of three using minimum comparisons
- Lexicographically middle string
- Product of middle row and column in an odd square matrix
- Replace the middle element of matrix with sum of surrounding elements
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- String formed with middle character of every right substring followed by left sequentially
- Print the middle character of a string
- Shortest Path using Meet In The Middle
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- 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
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.