This question evaluates basic string manipulation and character indexing skills, including handling of edge cases when treating input as a raw character array. Commonly asked in Coding & Algorithms interviews, it tests practical implementation ability and attention to indexing and boundary conditions, classifying the task as a practical application in the string-manipulation domain.
Given a non-empty string s with length at least 2, output a new string consisting of the last two characters of s in reverse order, separated by a single space.
s
(length
>= 2
).
s[last] + " " + s[second_last]
.
"bat"
"t a"
s
may contain letters, digits, or other characters; treat it as a raw character array/string.
"ab" -> "b a"
.