Skip to content

Instantly share code, notes, and snippets.

@jamiehs
Last active April 21, 2017 15:45
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jamiehs/29151b8086830c2d60532800a30298ed to your computer and use it in GitHub Desktop.
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
"foo bar baz qux".split.each { |word| puts "Hello " + word.capitalize }

credit: @wesbillman

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) => {
  console.log("Hello " + word.substr(0,1).toUpperCase() + word.substr(1))
})

credit: @kynatro

JavaScript (ES5)

var words = "foo bar baz qux".split(" ");
for(var i in words) {
  var word = words[i]
  console.log("Hello " + word.substr(0,1).toUpperCase() + word.substr(1))
}

credit: @kynatro

C#

var words = "foo bar baz qux".Split(' ');
foreach (var word in words)
{
    Console.WriteLine($"Hello {word.Substring(0, 1).ToUpper()}{word.Substring(1)}");
}

credit: @ymitis

Swift

"foo bar baz qux".components(separatedBy: " ").forEach { word in
    print("Hello \(word.capitalized)")
}

credit: @wesbillman

Kotlin

"foo bar baz qux".split(" ").forEach { println("Hello ${it.capitalize()}") }

credit: @wesbillman

Coffeescript

"foo bar baz qux".split(" ").forEach (word) ->
  console.log "Hello " + word[0].toUpperCase() + word[1..-1].toLowerCase()

credit: @wesbillman

Go

for _, word := range strings.Split("foo bar baz qux", " ") {
  fmt.Printf("Hello %s\n", strings.Title(word))
}

credit: @wesbillman

Objective C

NSArray* words = [@"foo bar baz qux" componentsSeparatedByString:@" "];
for (NSString* word in words) {
  printf("Hello %s\n", [[word capitalizedString] UTF8String]);
}

credit: @wesbillman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment