[01 Feb 2020] Reverse a string in Java
Reverse a string in Java
This article discusses 5 different ways to reverse a string in Java with examples.
Examples:

Examples:

Following are some interesting facts about String and StringBuffer classes :
1. Objects of String are immutable.
2. String class in Java does not have reverse() method, however StringBuilder class has built in reverse() method.
3. StringBuilder class do not have toCharArray() method, while String class does have toCharArray() method.
1. Objects of String are immutable.
2. String class in Java does not have reverse() method, however StringBuilder class has built in reverse() method.
3. StringBuilder class do not have toCharArray() method, while String class does have toCharArray() method.
Output:
skeeGrofskeeG
Using built in reverse() method of the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder. After that, print out the characters of the reversed string by scanning from the first till the last index.
Output:
skeeG rof skeeG
Output:
skeeGrofskeeG
Output:
skeeG roF skeeG
Comments
Post a Comment