<p>What is the easiest way to capitalize the first letter in each word of a string?</p>

[{'answer_id': 77237, 'author': 'zigdon', 'author_id': 4913, 'author_profile': 'https://Stackoverflow.com/users/4913', 'pm_score': 4, 'selected': False, 'text': '<p>Take a look at the <a href="http://perldoc.perl.org/functions/ucfirst.html" rel="noreferrer">ucfirst</a> function.</p>\n\n<pre><code>$line = join " ", map {ucfirst} split " ", $line;\n</code></pre>\n'}, {'answer_id': 77249, 'author': 'piCookie', 'author_id': 8763, 'author_profile': 'https://Stackoverflow.com/users/8763', 'pm_score': 5, 'selected': True, 'text': '<p>See <a href="http://learn.perl.org/faq/perlfaq4.html#How-do-I-capitalize-all-the-words-on-one-line" rel="nofollow noreferrer">the faq</a>.</p>\n\n<p>I don\'t believe <code>ucfirst()</code> satisfies the OP\'s question to capitalize the first letter of each word in a string without splitting the string and joining it later.</p>\n'}, {'answer_id': 77828, 'author': 'moritz', 'author_id': 14132, 'author_profile': 'https://Stackoverflow.com/users/14132', 'pm_score': 3, 'selected': False, 'text': '<pre><code>$string =~ s/(\\w+)/\\u$1/g;\n</code></pre>\n\n<p>should work just fine</p>\n'}, {'answer_id': 78165, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Note that the FAQ solution doesn't work if you have words that are in all-caps and you want them to be (only) capitalized instead. You can either make a more complicated regex, or just do a lc on the string before applying the FAQ solution.</p>\n"}, {'answer_id': 79379, 'author': 'RET', 'author_id': 14750, 'author_profile': 'https://Stackoverflow.com/users/14750', 'pm_score': -1, 'selected': False, 'text': '<p>The <a href="http://perldoc.perl.org/functions/ucfirst.html" rel="nofollow noreferrer">ucfirst</a> function in a map certainly does this, but only in a very rudimentary way. If you want something a bit more sophisticated, have a look at <a href="http://daringfireball.net/2008/08/title_case_update" rel="nofollow noreferrer">John Gruber\'s TitleCase script</a>.</p>\n'}, {'answer_id': 163826, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 6, 'selected': False, 'text': '<p>As @brian is mentioning in the comments the currently accepted answer by @piCookie is wrong!</p>\n\n<pre><code>$_="what\'s the wrong answer?";\ns/\\b(\\w)/\\U$1/g\nprint; \n</code></pre>\n\n<p>This will print "What\'S The Wrong Answer?" notice the wrongly capitalized S </p>\n\n<p>As the <a href="http://faq.perl.org/perlfaq4.html#How_do_I_capitalize_" rel="noreferrer">FAQ</a> says you are probably better off using </p>\n\n<pre><code>s/([\\w\']+)/\\u\\L$1/g\n</code></pre>\n\n<p>or  <a href="https://metacpan.org/module/Text::Autoformat" rel="noreferrer">Text::Autoformat</a></p>\n'}, {'answer_id': 182104, 'author': 'kixx', 'author_id': 11260, 'author_profile': 'https://Stackoverflow.com/users/11260', 'pm_score': 4, 'selected': False, 'text': '<pre><code>$capitalized = join \'\', map { ucfirst lc $_ } split /(\\s+)/, $line;\n</code></pre>\n\n<p>By capturing the whitespace, it is inserted in the list and used to rebuild the original spacing. "ucfirst lc" capitalizes "teXT" to "Text".</p>\n'}, {'answer_id': 2334645, 'author': 'vsync', 'author_id': 104380, 'author_profile': 'https://Stackoverflow.com/users/104380', 'pm_score': 1, 'selected': False, 'text': '<p>You can use \'<a href="http://daringfireball.net/2008/05/title_case" rel="nofollow noreferrer">Title Case</a>\', its a very cool piece of code written in Perl.</p>\n'}, {'answer_id': 9508413, 'author': 'alemol', 'author_id': 1241476, 'author_profile': 'https://Stackoverflow.com/users/1241476', 'pm_score': 2, 'selected': False, 'text': '<p>This capitalizes only the first word of each line:</p>\n\n<pre><code>perl -ne "print (ucfirst($1)$2)  if s/^(\\w)(.*)/\\1\\2/" file\n</code></pre>\n'}, {'answer_id': 47477440, 'author': 'bill god', 'author_id': 8557675, 'author_profile': 'https://Stackoverflow.com/users/8557675', 'pm_score': 1, 'selected': False, 'text': '<p>try this : </p>\n\n<pre><code>echo "what\'s the wrong answer?" |perl -pe \'s/^/ /; s/\\s(\\w+)/ \\u$1/g; s/^ //\'\n</code></pre>\n\n<p>Output will be:</p>\n\n<pre><code>What\'s The Wrong Answer?\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/77226', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13912/']