Skip to content

Instantly share code, notes, and snippets.

@wesbillman
Forked from jamiehs/in_this_language.md
Last active April 21, 2017 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesbillman/a55b66a481ca095800602900d18e549f to your computer and use it in GitHub Desktop.
Save wesbillman/a55b66a481ca095800602900d18e549f 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"].forEach { print("Hello \($0.capitalized)") }
"foo bar baz qux".components(separatedBy: " ").forEach { word in
    print("Hello \(word.capitalized)")
}

credit: @wesbillman

Kotlin

listOf("foo", "bar", "baz", "qux").forEach { println("Hello ${it.capitalize()}") }
"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

var words []string = []string{"foo", "bar", "baz", "qux"}

for _, word := range words {
  fmt.Printf("Hello %s\n", strings.Title(word))
}
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"];
for (NSString* word in words) {
  printf("Hello %s\n", [[word capitalizedString] UTF8String]);
}
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