Reverse a String in Java

In this tutorial, we will study 5 different ways to reverse a string in Java.

Reversing a String in Java is a very common operation and hence, one of the most frequently asked Java interview questions at beginner level interviews. So, let’s begin.

String Reverse in Java using For Loop

In this approach, we are using two string methods, charAt()  and length(). The charAt(index) method means ‘character at’. In the place of the index, the integer value is inserted from 0 to length-1 of the string.

So, the charAt() will return the character of the string which will be present at that index. The length() method returns an integer value of the length of the string evoked.

package com.artoftesting.java;
public class StringReverse {
public static void main (String[] args)	{

//String to be reversed
String str = "testing";

//Length of the string
int length = str.length();

//Initializing the reversed string variable
String reverse = "";

for(int i=length-1;i>=0;i--)
reverse= reverse + str.charAt(i);

System.out.println(reverse);
}
}

Reverse a String in Java using Recursion

If a function calls itself multiple times then such an approach is called a recursive method.
It is important to know a recursive approach to a problem as it breaks down a code into small repetitive blocks. Thus making it easier to understand by making the code look less complicated. Keep in mind, in a recursion a base condition and a terminating condition is a must, otherwise the function will call itself indefinitely.

package com.artoftesting.java;
public class StringReverse {

//method which reverses the string
private static String reverse(String str) {

//terminating condition
if(str == null || str.length() <= 1) {
return str;
}

//calling the recursive function
return reverse(str.substring(1)) + str.charAt(0);
}

public static void main(String[] args) {

String str = "testing";

//using the reverse method of string
String rev = reverse(str);
System.out.println(rev);
}
}

String Reverse using StringBuffer Class

The StringBuffer class is just like the string class, with a slight difference – it can be modified. The objects in string buffer class are mutable.

In this approach, we can simply use the reverse() method of StringBuffer class.

package com.artoftesting.java;
public class StringReverse {

public static void main(String[] args) {

String str = "testing";

//creating an object for the string buffer class
StringBuffer sb= new StringBuffer(str);

//using reverse() method of the String builder
System.out.println(sb.reverse());
}
}

String Reverse using StringBuilder Class

The StringBuilder class is a faster alternative to the StringBuffer class but it is not thread safe. So, if thread safety is not required then we should opt for StringBuilder class.

In this approach, we can simply use the reverse() method of StringBuilder class to reverse a string.

package com.artoftesting.java;
public class StringReverse {

public static void main(String[] args) {

String str ="testing";

//creating an object for the string builder class
StringBuilder sb = new StringBuilder(str);

//using reverse() method of the String
System.out.println(sb.reverse());
}
}

Using toCharArray() and Concat operator

The toCharArray() method means to character array and is used to covert a string or a number into an array of characters. This helps in operating the characters of the string individually. The Concat operator(+) is used to join two strings.

package com.artoftesting.java;
public class StringReverse {

public static String reverseString(String str){

//convert the string into a character array
char charArray[] = str.toCharArray();

String reverse = "";

//for loop to reverse a string
for(int i=charArray.length-1;i>=0;i--){

//concat operator(+)
reverse = reverse + charArray[i];
}

return reverse;
}

public static void main(String[] args) {

System.out.println(reverseString("testing"));

}
}

With this, we have come to an end of this tutorial, covering 5 of the most common ways to reverse a string in Java.

От QA genius

Adblock
detector