Reverse a String | Practice | GeeksforGeeks

Given a String S , print the reverse of the string as output.

Example 1:
Input: S = "GeeksforGeeks"
Output: "skeeGrofskeeG" 
Explanation: Element at first is at last and
last is at first, second is at second last and 
second last is at second position and so on .

Example 2:
Input: S = "ReversE"
Output: "EsreveR"
Explanation: "E" at first and "R" at last and
"e" at second last and "s" at second and
so on .

Your Task:  
You dont need to read input or print anything. Complete the function revStr() which takes S as input parameter and returns the reversed string .

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)

Constraints:
1<= |S| <=1000
+1
samisalgaonkar1 week ago
Python:
return S[::-1]
-1
ksvijayan062 weeks ago
Java Loop approach
 
class Solution {
    static String revStr(String S) {
        // code here
        char[] arr = S.toCharArray();
        for(int i =0, j =arr.length-1; i<j; i++, j--){
            char temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        String s = new String(arr);
        return s;
    }

}
0
sachinmishraravi3 weeks ago
java
        StringBuilder sb= new StringBuilder(S);
        
        sb.reverse();
        return sb.toString();
+2
19tuec1323 weeks ago
//single line 
return new StringBuilder(S).reverse().toString();
0
purisaurabh28854 weeks ago
string revStr(string S)    {       // code here(       int start  = 0;       int end = S.size()-1;              while(start < end)       {           swap(S[start] , S[end]);           start++;           end--;       }              return S;   }
+1
akashboswas21 month ago
class Solution {   static String revStr(String S) {       // code here                     StringBuilder b=new StringBuilder(S);       b.reverse();                     return b.toString();   }}
0
whitehatonion1 month ago
class Solution {   static String revStr(String S) {       // code here     StringBuilder a = new StringBuilder(S);     a.reverse();     return a.toString();   }}
-1
neelasatyasai931 month ago
#PYTHON
class Solution:   def revStr (ob, S):       return S[: : -1]
+1
atif836141 month ago
String res = ""; for(int i=S.length()-1;i>=0;i--) {  res  = res +S.charAt(i); } return res;   }
0
sonalyadav452 months ago
static String revStr(String S) {       // code here      StringBuffer reverseStr = new StringBuffer(S);      return reverseStr.reverse().toString();   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.