Instantly share code, notes, and snippets.

Embed
What would you like to do?
In This Language (add your comments or suggestions)

Input

foo bar baz qux

#Required Output

Hello Foo
Hello Bar
Hello Baz
Hello Qux

PHP

$some_words = "foo bar baz qux";

$some_words_parts = explode(' ', $some_words);
foreach($some_words_parts as $word) {
  $title_case_word = ucwords($word);
  echo "Hello {$title_case_word}\n";
}

Ruby

%w(foo bar baz qux).each do |word|
  title_case_word = word.gsub(/\b('?[a-z])/) { $1.capitalize }
  puts "Hello #{title_case_word}\n"
end

Python

some_words = "foo bar baz qux".split(' ')
for word in some_words:
  print "Hello %s" % word.title()

JavaScript (ES6)

"foo bar baz qux".split(" ").forEach((word) => {
  const phrase = ["Hello", word]
  console.log(phrase.map((p) => `${p.substr(0,1).toUpperCase()}${p.substr(1)}`).join(" "))
})

JavaScript (ES5)

var words = "foo bar baz qux".split(" ");
for(var i in words) {
  var phrase = ["Hello", words[i]]
  var formatted = []
  for(var ii in phrase) {
    var word = phrase[ii]
    formatted.push(word.substr(0,1).toUpperCase() + word.substr(1))
  }
  console.log(formatted.join(" "))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment