0
How do you take a reverse action on a string?
Lets say you have string value of "how",i want to print "woh"..in reverse order
5 Respostas
+ 2
Many ways but simply use loop start print values from index string.length-1th to 0th index value.
+ 3
You can also create a StringBuilder object that takes the string as an argument of its constructor then simply use .reverse () method
+ 2
U can use predefined method or u can do it via loop start loop from total length of string to till 0
+ 1
Reverse a String using CharAt Method
In the below given Java program will help you to understand how to reverse a String entered by the user. Here, I have used the CharAt() method in order to extract the characters from the input String. The main task of the CharAt() method is to return the character at the specified index in the given String. Then, I have appended them in reverse order to reverse the given String. It is one of the simple approaches to reverse a String in Java.
If you want to take a (small) step further, here's a Java 8+ one-liner:
Function<String, String> reverse = s -> new StringBuilder(s).reverse().toString();