There are many ways of reversing a String in Java for whatever reason you may have. Today, we will look at a few simple ways of reversing a String in Java. For example, the string “Reverse Me” once reversed will be “eM esreveR”. We will start by looking at the most traditional method with the least help from external Java classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; public class ReverseString { public static void main(String[] args) { System.out.println("Enter string to reverse:"); Scanner read = new Scanner(System.in); String str = read.nextLine(); String reverse = ""; for(int i = str.length() - 1; i >= 0; i--) { reverse = reverse + str.charAt(i); } System.out.println("Reversed string is:"); System.out.println(reverse); } } |
In the above code, we are basically reading in a String from the user afterwhich we will begin an iteration loop that will build the new reversed String. This is done in the “for” loop by obtaining the characters of the original String individually from the end by using the “charAt” function of the String class and concatenating them to a new String by using the “+” operator.
Another method which is also similar but uses the Java StringBuilder class instead:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; public class ReverseString { public static void main(String[] args) { System.out.println("Enter string to reverse:"); Scanner read = new Scanner(System.in); String str = read.nextLine(); StringBuilder sb = new StringBuilder(); for(int i = str.length() - 1; i >= 0; i--) { sb.append(str.charAt(i)); } System.out.println("Reversed string is:"); System.out.println(sb.toString()); } } |
Very similar to the previous method, the only difference is that we are using the “append” function of the Java StringBuilder class in the iteration loop instead to concatenate the characters to form the reversed String.
Of course there is still an easier shortcut to do it and that would be to simply use the Java StringBuilder class “reverse” function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Scanner; public class ReverseString { public static void main(String[] args) { System.out.println("Enter string to reverse:"); Scanner read = new Scanner(System.in); String str = read.nextLine(); StringBuilder sb = new StringBuilder(str); System.out.println("Reversed string is:"); System.out.println(sb.reverse().toString()); } } |
What we have seen above are just a few ways to reverse a String in Java. The point is that there are many possible ways to reverse a String in Java and at the end of the day, it boils down to individual requirements, restrictions and what works best for you.
good explanation….
String cant reverse in java..if u put a first program the output wont show.
write first outut statement as System.out.print(” ” ); remove ln
and you r good to go
In java the string cant reverse..if u type the first program the output wont show..it means there is not a reverse string..
class StringReverse
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char[] c=s.toCharArray();
for(int i=c.length;i>=1;i–)
{
System.out.print(c[i-1]);
}
}
}
i need reverse striing …using iterator also
Another method could also be used is:
public static void main (String args[])
{
String s=”This is a string”;
char arr[]=s.toCharArray(); // convert a string into character array and save it in a variable
// check if the string is empty or null
if(s.isEmpty()|| s ==null)
{
System.out.println(“Empty string is not accepted “);
}
// print the string in reverse order
for(int i = arr.length-1; i>=0; i–)
{
System.out.print(arr[i]);
}
System.out.println(” “);
}
}
program simple hona chahiye jisse koi v smajh ske …..so, this is so simple..thanku
Another method is:
public static void main (String args[])
{
String s1=”This is a string”;
int size = s1.length();
String reverse = “”;
// print the string in reverse order
for(int i = size-1; i>=0; i–)
{
reverse = reverse + s1.charAt(i);
}
System.out.println(reverse);
}
}
why do you need for loop…..
Easy Understandable simple .. very good
How would you change the code so that it reverses the order of the string, rather than the words in the string ?
How would you change the code so that it reverses the order of the array rather than the words ?
Try Stack to Reverse a String
By importing java.util.Stack;
Is it possible without using another String or char[] we can reverse a string in same string ?
Last element in the string is not \0, then why wrote there length() -1 in loop
Is it possible without using another String or char[] we can reverse a string in same string ?