Reverse the given string in the range [ L , R ] | C ++ implementation of the approach ; Function to return the string after reversing characters in the range [ L , R ] ; Invalid range ; While there are characters to swap ; Swap ( str [ l ] , str [ r ] ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string reverse ( string str , int len , int l , int r ) { if ( l < 0 r > = len l > r ) return str ; while ( l < r ) { char c = str [ l ] ; str [ l ] = str [ r ] ; str [ r ] = c ; l ++ ; r -- ; } return str ; } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; int l = 5 , r = 7 ; cout << reverse ( str , len , l , r ) ; return 0 ; }