Reverse a String: 4 potential approaches

This is probably one of the most common Java programming interview question and there are at least 4 different approaches that can be taken to tackle it. We will illustrate them by presenting some examples in Java.

1) Use the built-in StringBuffer reverse() method:

public class ReverseStringBuffer
{
    public static void main(String args[])
    {
        String originalString = "MyAwesomeString";
        String reversedString = new StringBuffer(originalString).reverse().toString();
        System.out.println("This is the original String " + originalString +
        "n" + "This is the reversed String " + reversedString);
    }
}

2) Use the built-in StringBuilder reverse() method:

public class ReverseStringBuilder
{
    public static void main(String args[])
    {
        String anotherOriginalString = "AmazingString";
        StringBuilder anotherReversedString = 
            new StringBuilder(anotherOriginalString).reverse();
        System.out.println("This is the original String " + anotherOriginalString + 
            "n" + "This is the reversed String " + anotherReversedString);
    }
}

3) Convert a String into a Char array and loop through each character.

public class ReverseStringCharArray
{
    public static void main(String args[])
    {
        String myString = "ThisIsMyAwesomeString";
        char[] stringToChar = myString.toCharArray();
        System.out.println(stringToChar.length);
        for(int i = stringToChar.length - 1; i >= 0; i--)
        {
	        System.out.print(stringToChar[i]);
        }
A similar implementation can be re-written as an utility:
 
public class ReverseStringCharArray
{
    public static void main(String args[])
    {
        String term = "HappyString";
        //Here we are calling the reverse utility method
        String reversedTerm= reverse(term);
        System.out.println("This is the original String " + term + 
            "n" + "This is the reversed String " + reversedTerm);
    }

    public static String reverse(String sourceTerm)
    {
        if(sourceTerm == null || sourceTerm.isEmpty())
        {
	        return sourceTerm;
        }       
	    String reversedString = "";
	    for(int i = source.length() -1; i>= 0; i--)
        {
	        reversedString = reversedString + source.charAt(i);
	        System.out.println("This prints the reversed string " + 
                reversedString + " n " + 
                " whereas this prints the individual character " + source.charAt(i));
	    }     
	    return reversedString;
    }    
}

4) Use recursion to reverse the entered String. This method returns the reverse of the string passed in by appending the first character (sourceTern.charAt(0)) to the remainder of the String itself (str.substring(1)). If the String is less or equal to 1 character, then the recursion is halted.

public class ReverseStringRecursiveMethod
{
    public static void main(String args[])
    {
        String term = "AnotherString";
        //prints the result of the call to the recursive method
        System.out.println(recursiveMethod(term)); 
    }

    public static String recursiveMethod(String sourceTerm)
    {
        if ( (null == sourceTerm) || (sourceTerm.length() <= 1) )
        {
            return sourceTerm;
        }
        
        return recursiveMethod(sourceTerm.substring(1)) + sourceTern.charAt(0);
    }
}
Advertisements

Posted on November 16, 2015, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: