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

For example,

input:-"hellojava"

output:- "avajolleh"

有帮助吗?

解决方案

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();

其他提示

Try the next:

String output = new StringBuilder(input).reverse().toString();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top