How can I capitalize the first letter of each word in a string in Perl?
Take a look at the ucfirst function.
$line = join " ", map {ucfirst} split " ", $line;
As @brian is mentioning in the comments the currently accepted answer by @piCookie is wrong!
$_="what's the wrong answer?";
s/\b(\w)/\U$1/g
print;
This will print "What'S The Wrong Answer?" notice the wrongly capitalized S
As the FAQ says you are probably better off using
s/([\w']+)/\u\L$1/g
or Text::Autoformat
See the faq.
I don't believe ucfirst()
satisfies the OP's question to capitalize the first letter of each word in a string without splitting the string and joining it later.