Question

I need a java program which reverse a string with using methods.

For example,

input:-"hellojava"

output:- "avajolleh"

Was it helpful?

Solution

You can directly use

String word= "hellojava";

new StringBuilder(word).reverse().toString();

for one word you need below one.

String word= "hello java";

for (String part : word.split(" ")) {
    System.out.print(new StringBuilder(part).reverse().toString());
    System.out.print(" ");
}

This is supported since java 1.5 ealier versions you should use StringBuffer. And for any Java based project you can use this method.

new StringBuffer (word).reverse().toString();

OTHER TIPS

Try the next:

String output = new StringBuilder(input).reverse().toString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top