Java Program to Reverse a String using For Loop


However odd it may sound, but the basic logic of reversing a String continues to be one of the most asked Interview questions. You'll find this question in various forms, with slight twists in a lot of interviews that require coding. Its an important Interview question for Java.

Why? Because, it involves two very important concepts of Java, the use of Strings, and the use of a for loop. 

If a recruiter wants to check your basic knowledge of coding, then String and loops are his best bet.

Today we'll see how to solve this problem of reversing a String using for loop.

There can be many approaches to solving this problem. 

We'll try to solve it using No Extra Libraries, and our basic knowledge of For loop and String functions.

We'll declare a function to take the String as an Input. 

public String reverseString(String input)

Next, we'll declare another String where we are going to store the reversed string. 

//Create a String with no data
String output = "";

Now, we need to loop through the input string. Now, because we have to reverse the String, we have to start from the last character of the String, and continue till we reach the first character. 

This can easily be achieved using a for loop. We will be using inbuilt length() function to find the length of the input String on the fly.

for(int i=input.length()-1;i>=0;i--)

Also, each character of the String needs to be read and stored into output. We could have used Character Arrays here, but then it will complicate things. If we use the array approach, then we need to convert the String to Character Array, do the operations and convert back to String. Why the hassle!

There is a inbuilt String function that will solve our purpose very easily. Its called charAt(int index).

We can simply pass the index of the String character we want to process and the function will return it. We are already using the for loop to move across each character, so it suits our purpose perfectly!

//store each character into the output string.
output = output + input.charAt(i);

That's it! Now let's take a look at the complete code.

package coding;

public class ReverseString {

	public static void main(String[] args) {
		ReverseString rs = new ReverseString();
		rs.reverseString("Hello World!");

	 * Reverse a String using for loop
	 * @author
	 * @param input
	 * @return
	public String reverseString(String input) {
		System.out.println("Input String: "+input);
		//Create a String with no data
		String output = "";
		//Loop through the Input String from the last character and come to the first character. 
		for(int i=input.length()-1;i>=0;i--) {
			//store each character into the output string.
			output = output + input.charAt(i);
		System.out.println("Reversed String: "+output);
		return output;


That's pretty compact isn't it? And take a look at the output as well.

Input String: Hello World!

Reversed String: !dlroW olleH

That's it. Now we have a reusable function of our own, that can be used across any code assignment that requires reversing a String!

Thanks for Reading the Article. If you have reached this far, we hope that the article was useful to you! Please Like/Share/Follow us on FacebookTwitterTumblr.


Popular posts from this blog

Calculate Your CTC Salary Hike Percentage - Online Calculator to find your New Salary

Find the Longest Streak of 1's in a Binary Array using Java Code

Java Program to read Excel File and Load into Array

Java Program to Read a Properties file and return a Property Value

Create a File logging Utility using Java Code