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:
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.
- Check if a given string is a Reverse Bitonic String or not
- Reverse the substrings of the given String according to the given Array of indices
- Reverse words in a given string
- First string from the given array whose reverse is also present in the same array
- Program to reverse words in a given string in C++
- Count of Reverse Bitonic Substrings in a given String
- Reverse words in a given string | Set 2
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Reverse a string without affecting special characters
- Different methods to reverse a string in C/C++
- Print words of a string in reverse order
- PHP | Reverse a String
- Print reverse string after removing vowels
- Reverse String according to the number of words
- Reverse middle words of a string
- Add index to characters and reverse the string
- Reverse alternate k characters in a string
- Reverse every word of the string except the first and the last character
- First substring whose reverse is a word in the string
- Reverse all the word in a String represented as a Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.