Jul 4, 2024
toCharArray()
Method"HELLO"
becomes ['H', 'E', 'L', 'L', 'O']
.String str = "HELLO";
char[] charArray = str.toCharArray();
for (int i = charArray.length - 1; i >= 0; i--) {
System.out.print(charArray[i]);
}
OLLEH
charAt()
MethodcharAt()
method of String class.String str = "HELLO";
for (int i = str.length() - 1; i >= 0; i--) {
System.out.print(str.charAt(i));
}
OLLEH
reverse()
Method of StringBuffer
ClassStringBuffer
object and call its reverse()
method.StringBuffer sb = new StringBuffer("HELLO");
System.out.println(sb.reverse());
OLLEH
reverse()
Method of StringBuilder
ClassStringBuffer
, but using StringBuilder
.StringBuilder sb = new StringBuilder("HELLO");
System.out.println(sb.reverse());
OLLEH
reverse()
method.toCharArray()
) or Approach 2 (charAt()
).