Given a string str, and two integers L and R, the task is to reverse the string in the range [L, R] i.e. str[L…R].
Input: str = “geeksforgeeks”, L = 5, R = 7
Reverse the characters in the range str[5…7] = “geeksforgeeks”
and the new string will be “geeksrofgeeks”
Input: str = “ijklmn”, L = 1, R = 2
- If the range is invalid i.e. either L < 0 or R ≥ len or L > R then print the original string.
- If the range is valid then keep swapping the characters str[L] and str[R] while L < R and update L = L + 1 and R = R – 1 after every swap operation. Print the updated string in the end.
Below is the implementation of the above approach:
- Create a new string by alternately combining the characters of two halves of the string in reverse
- String Range Queries to find the number of subsets equal to a given String
- PHP | Reverse a String
- Different methods to reverse a string in C/C++
- Reverse words in a given string
- Reverse vowels in a given string
- Reverse a string in Java
- Reverse string without using any temporary variable
- Add index to characters and reverse the string
- First substring whose reverse is a word in the string
- Reverse alternate k characters in a string
- Reverse words in a given String in Python
- Reverse middle words of a string
- Reverse string in Python (5 different ways)
- Reverse every word of the string except the first and the last character
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.