#K57307. Capitalize Words
Capitalize Words
Capitalize Words
Given a string s which may contain leading, trailing, or multiple spaces, write a program that capitalizes the first letter of each word and converts the remaining letters of the word to lowercase. Words are defined as sequences of non-space characters separated by one or more spaces. The program should also remove any extra spaces so that words are separated by a single space.
For example, if the input is hello world
, the output should be Hello World
. In case the input contains only spaces, the output should be an empty string.
Note: When writing your solution, if you use any mathematical expressions refer to them in LaTeX format. For example, the length of the input string can be denoted as $|s|$.
inputFormat
The input is read from stdin
as a single string. This string may contain leading or trailing spaces and multiple consecutive spaces between words.
Example:
hello world this is a test
outputFormat
The output should be a single string printed to stdout
with each word capitalized and separated by a single space. All extra spaces should be removed.
Example:
Hello World This Is A Test## sample
hello world
Hello World