Given a string s, perform four string operations without using any built-in string library functions (no split, reverse, strip, join and so on). Implement every operation from scratch by iterating over the string character by character.
Return a list of four strings:
-
The reversed string of
s
.
-
The number of words in
s
, written as a string. Words are separated by one or more spaces.
-
The trimmed string, with all leading and trailing spaces removed.
-
The string with each word reversed in place (word order preserved, words separated by single spaces, no leading or trailing spaces).
A word is a maximal sequence of non-space characters.
Function Signature
def manipulate_string(s: str) -> list[str]:
Rules
-
The result has exactly four entries, in the order listed above.
-
The word count is written in decimal with no padding, for example
"2"
.
-
Trimming removes spaces only at the two ends. Spaces between words stay exactly as they appear in
s
.
-
The only whitespace character is the space
' '
.
-
If
s
contains only spaces, the word count is
"0"
and both the trimmed string and the word-reversed string are empty.
Constraints
-
1 <= len(s) <= 10^4
-
s
consists of English letters, digits and spaces.
Examples
Input: s = " hello world "
Output: [" dlrow olleh ", "2", "hello world", "olleh dlrow"]
The full reversal still has two spaces at each end because s has two at each end. There are two words, trimming drops the edge spaces, and reversing each word in place gives "olleh dlrow".
Input: s = "abc def ghi"
Output: ["ihg fed cba", "3", "abc def ghi", "cba fed ihg"]
There are no edge spaces, so the trimmed string equals s. Reversing each of the three words keeps their order, while the full reversal also flips the word order.
Input: s = "a bc "
Output: [" cb a", "2", "a bc", "a cb"]
Trimming removes only the trailing space and keeps the double space between the words. The word-reversed string puts a single space between "a" and "cb".