I need to reverse the string of a user's input.
I need it done in the simplest of ways. I was trying to do reverseOrder(UserInput) but it wasn't working.
For example, user inputs abc I just take the string and print out cba
I need to reverse the string of a user's input.
I need it done in the simplest of ways. I was trying to do reverseOrder(UserInput) but it wasn't working.
For example, user inputs abc I just take the string and print out cba
new StringBuilder(str).reverse().toString()
java.util.Collections.reverseOrder
is for sorting in reverse of normal order.
I prefer using Apache's commons-lang for this kind of thing. There are all kinds of goodies, including:
StringUtils.reverse("Hello World!");
yields: !dlroW olleH
StringUtils.reverseDelimited("Hello World!", ' ');
yields: World! Hello
If you are new to programming, which I guess you are, my suggestion is "Why use simple stuff?". Understand the internals and play some!!
public static void main(String[] args) {
String str = "abcasz";
char[] orgArr = str.toCharArray();
char[] revArr = new char[orgArr.length];
for (int i = 0; i < orgArr.length;i++) {
revArr[i] = orgArr[orgArr.length - 1 - i];
}
String revStr = new String(revArr);
System.out.println(revStr);