0

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

For example,

input:-"hellojava"

output:- "avajolleh"

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
davut
  • 41
  • 1
  • 7

2 Answers2

2

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();
Lasitha Benaragama
  • 2,201
  • 2
  • 27
  • 43
1

Try the next:

String output = new StringBuilder(input).reverse().toString();
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148