Reverse a String Java

 


public class StringReverseExample {
public static void main(String args[]) {
String word = "HelloWorld";
String reverse = new StringBuffer(word).reverse().toString();
System.out.println(reverse);

word = "WakeUp";
reverse = new StringBuilder(word).reverse().toString();
System.out.println(reverse);

word = "Band";
reverse = reverse(word);
System.out.println(reverse);
}
public static String reverse(String source) {
if(source == null || source.isEmpty()) {
return source;
}
String reverse = "";
for(int i = source.length() -1; i>=0; i--) {
reverse = reverse + source.charAt(i);
}
return reverse;
}
}

Comments

Popular posts from this blog

Android - Using KeyStore to encrypt and decrypt the data

Stack and Queue

Java Reflection API