How to Reverse a String in Java

How to Reverse a String in Java

This is part of a series of practice problems and projects in Java. To read more about this series and why it began, start here.

The Problem

Given a word or sentence (aka a string), write a program that will reverse the string and display the output as a string.

For example, given the word 'hello', the output should be 'olleh'.

Pseudocode

Like most programming problems, there are many ways to go about solving them.

This is how I think about this problem to solve it.

First, if I'm given a string, my first thought is to think about how I can loop through the string.

The simplest way I can think about looping through anything is by using a loop. Based on my very new knowledge of Java, I'm thinking there are probably two ways to loop through the string:

  1. To either loop through the string directly

OR

  1. Convert the string to an array and loop through the array

I find it easier to work with arrays than to loop through the string directly, so I'm going to go with option 2.

So, my first pseudo code will be:

//convert string to array list

Now, once the string is in an array, I'll need to loop through the array. However, how will I reverse the string so each character is stored in reverse?

My initial thought is to create a new array to store each reversed character. This means, as I loop through the reversed string, I'll store each value in the new array I create.

I'm also thinking when I loop through the reversed array, I'll need to loop through starting at the end of the array, in order to store the characters in reversed order.

This now makes my pseudocode this:

//create a new array list to store final string

//loop through char array starting at end

As I loop through each character starting at the end of the array, I'll need to add each character to the new array I created.

So, pseudocode is now:

//add each value from converted list in reversed list

At this point, the new array that I'm now calling the reversed list, will have the reversed characters stored inside of it.

The last step is to display the output as a string. The values are in an array, so I'm thinking I need to convert the array back to a string and then output the string.

Final pseudocode is:

//convert final array back to string and return string

Resolution

package practice.reverseString;
import java.util.*;

public class ReverseString {
    public static void main(String[] args) {
        // given a string reverse it
        // hello becomes olleh
        System.out.println("Enter a word or sentence to reverse:");
        Scanner scan = new Scanner(System.in);
        String enteredString = scan.nextLine();
        ReverseAString reverse = new ReverseAString();
        reverse.reverseStrings(enteredString);        
    }
}

class ReverseAString {
    String givenString = "";

    public void reverseStrings(String sentence) {
        givenString = sentence;

        //convert string to array list 
        List<String> stringToArray = new ArrayList<String>(Arrays.asList(givenString.split("")));

        //create a new array list to store final string
        List<String> finalArray = new ArrayList<String>();

        //loop through char array starting at end
        for(int i = stringToArray.size() - 1; i >= 0; i--) {    
            //add each value from converted list in reversed list
            finalArray.add(stringToArray.get(i));
        }

        //convert final reversed array list back to string and output the reversed string
        String arrayListToString = String.join("", finalArray);
        System.out.println(arrayListToString);
    }
}

Next Steps

If you liked this article or it was helpful to you, check out other topics in the series - Java Practice Series.