In this article we are going to discuss a problem from Java String; this is one of the most commonly asked interview question of today. Write a program in Java to reverse a String without using reverse() and without converting it to array.
 

Write a program in Java to reverse a String without using reverse() and without converting it to array.

e.g. if given string is "tbNext" than reverse it to "sprubhcet", without reverse() method and without converting it to array.
We can reverse a string by iterating it throw out from behind and printing elements using charAt() function as shown in below code:
package com.tbNext.byexample;

public class TBReverseStringDemo {

	public static void main(String[] args) {

		TBReverseStringDemo reverseStringDemo = new TBReverseStringDemo();
		System.out.println("Reversed String: "
				+ reverseStringDemo.reverseString("tbNext"));
	}

	private String reverseString(String inputString) {
		String reversedString = "";
		for (int i = inputString.length() - 1; i > -1; i--)
			reversedString += inputString.charAt(i);
		return reversedString;
	}
}


Output:

Reversed String: sprubhcet


That'?s it, we saw how to write a program in Java to reverse a String without using reverse() and without converting it to array. In upcoming articles we will see more about Java and Related programming problems and solutions.
  • By Techburps.com
  • May 9, 2015
  • Programming