Reverse a String

Reverse a String


Problem Statement

You are given a string s. You need to reverse the string.

Example 1

Input :
s = Geeks

Output : skeeG

Example 2

Input :
s = for

Output : rof

Task

You only need to complete the function reverseWord() that takes s as parameter and returns the reversed string.

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

Constraints :
1 <= |S| <= 10000



Solutions

Java Solution


class Reverse
{
    // Complete the function
    // str: input string
    public static String reverseWord(String str)
    {
        // Reverse the string str
        String temp = str;
        String revStr = "";
        for(int i=str.length()-1; i>=0; i--) {
            revStr = revStr + temp.charAt(i);
        }
        return revStr;
    }
}     
    

Post a Comment

0 Comments