Search code examples
javastringeclipsereverseacm-java-libraries

How can i write java reverse string program in ACM Library


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

For example,

input:-"hellojava"

output:- "avajolleh"


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