Reverse the given string in the range [L, R] Last Updated : 19 Oct, 2022 Comments Improve Suggest changes Like Article Like Report 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].Examples: Input: str = "geeksforgeeks", L = 5, R = 7 Output: geeksrofgeeks Reverse the characters in the range str[5...7] = "geeksforgeeks" and the new string will be "geeksrofgeeks" Input: str = "ijklmn", L = 1, R = 2 Output: ikjlmn Approach: 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: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the string after // reversing characters in the range [L, R] string reverse(string str, int len, int l, int r) { // Invalid range if (l < 0 || r >= len || l > r) return str; // While there are characters to swap while (l < r) { // Swap(str[l], str[r]) char c = str[l]; str[l] = str[r]; str[r] = c; l++; r--; } return str; } // Driver code int main() { string str = "geeksforgeeks"; int len = str.length(); int l = 5, r = 7; cout << reverse(str, len, l, r); return 0; } Java // Java implementation of the approach import java.util.*; class GFG { // Function to return the string after // reversing characters in the range [L, R] static String reverse(char[] str, int len, int l, int r) { // Invalid range if (l < 0 || r >= len || l > r) return "Invalid range!"; // While there are characters to swap while (l < r) { // Swap(str[l], str[r]) char c = str[l]; str[l] = str[r]; str[r] = c; l++; r--; } String string = new String(str); return string; } // Driver code public static void main (String[] args) { String str = "geeksforgeeks"; int len = str.length(); int l = 5, r = 7; System.out.println(reverse(str.toCharArray(), len, l, r)); } } // This code is contributed by Ashutosh450 Python3 # Python3 implementation of the approach # Function to return the string after # reversing characters in the range [L, R] def reverse(string, length, l, r) : # Invalid range if (l < 0 or r >= length or l > r) : return string; string = list(string) # While there are characters to swap while (l < r) : # Swap(str[l], str[r]) c = string[l]; string[l] = string[r]; string[r] = c; l += 1; r -= 1; return "".join(string); # Driver code if __name__ == "__main__" : string = "geeksforgeeks"; length = len(string); l = 5; r = 7; print(reverse(string, length, l, r)); # This code is contributed by AnkitRai01 C# // C# implementation of the approach using System; class GFG { // Function to return the string after // reversing characters in the range [L, R] static String reverse(char[] str, int len, int l, int r) { // Invalid range if (l < 0 || r >= len || l > r) return "Invalid range!"; // While there are characters to swap while (l < r) { // Swap(str[l], str[r]) char c = str[l]; str[l] = str[r]; str[r] = c; l++; r--; } return String.Join("",str); } // Driver code public static void Main (String[] args) { String str = "geeksforgeeks"; int len = str.Length; int l = 5, r = 7; Console.WriteLine(reverse(str.ToCharArray(), len, l, r)); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of the approach // Function to return the string after // reversing characters in the range [L, R] function reverse(str, len, l, r) { // Invalid range if (l < 0 || r >= len || l > r) return "Invalid range!"; // While there are characters to swap while (l < r) { // Swap(str[l], str[r]) let c = str[l]; str[l] = str[r]; str[r] = c; l++; r--; } return str.join(""); } let str = "geeksforgeeks"; let len = str.length; let l = 5, r = 7; document.write(reverse(str.split(''), len, l, r)); // This code is contributed by divyeshrabadiya07. </script> Output: geeksrofgeeks Time complexity: O(N), where N = (r - l)/2Auxiliary space: O(1) Comment More infoAdvertise with us Next Article Reverse given range of String for M queries K koulick_sadhu Follow Improve Article Tags : Strings DSA Reverse substring Practice Tags : ReverseStrings Similar Reads Reverse given range of String for M queries Given a string S of length N and an array of queries A[] of size M, the task is to find the final string after performing M operations on the string. In each operation reverse a segment of the string S from position (A[i] to N-A[i]+1). Examples: Input: N = 6, S = "abcdef", M = 3, A = {1, 2, 3}Output 15 min read Program to reverse words in a given string in C++ Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++. Examples: Input: str = "the sky is blue" Output: blue is sky theInput: str = "I love programming" Output: programming love I Method 1: Using STL functions Reverse the given string str using STL 6 min read Program to reverse words in a given string in C++ Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++. Examples: Input: str = "the sky is blue" Output: blue is sky theInput: str = "I love programming" Output: programming love I Method 1: Using STL functions Reverse the given string str using STL 6 min read Program to Reverse the subarray in between given range Given an array arr and a range [L, R], the task is to reverse the subarray in between the given range [L, R]. Examples: Input: arr = [1, 2, 3, 4, 5, 6, 7], L = 1, R = 5Output: [1, 6, 5, 4, 3, 2, 7] Input: arr = [10, 20, 30, 40, 50], L = 0, R = 2Output: [30, 20, 10, 40, 50] Recommended PracticeRevers 7 min read Reverse Words in a Given String in Python In this article, we explore various ways to reverse the words in a string using Python. From simple built-in methods to advanced techniques like recursion and stacks. We are going to see various techniques to reverse a string.Using split() and join()Using split() and join() is the most common method 2 min read Reverse the string whenever digit is encountered Given a string s of length N, the task is to traverse the string and reverse the substring from the start to the next encountered digit. After reversing the substring, update the start index to the character immediately following the digit. Example: Input: s = abc123def456ghiOutput: cba123fed456ihg 4 min read Like