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:
- Reverse middle words of a string
- Sorting array with reverse around middle
- Add index to characters and reverse the string
- Reverse alternate k characters in 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
- 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
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Middle of three using minimum comparisons
- Lexicographically middle string
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.