0
0
RubyProgramBeginner · 2 min read

Ruby Program to Capitalize Each Word

Use the Ruby method split to break the string into words, then map(&:capitalize) to capitalize each word, and finally join(' ') to combine them back into a string, like this: str.split.map(&:capitalize).join(' ').
📋

Examples

Inputhello world
OutputHello World
Inputruby programming language
OutputRuby Programming Language
Inputa quick brown fox
OutputA Quick Brown Fox
🧠

How to Think About It

To capitalize each word in a sentence, first split the sentence into individual words using spaces as separators. Then, for each word, change the first letter to uppercase and keep the rest lowercase. Finally, join all the capitalized words back together with spaces to form the new sentence.
📐

Algorithm

1
Get the input string.
2
Split the string into a list of words using spaces.
3
For each word in the list, capitalize the first letter.
4
Join all the capitalized words back into a single string with spaces.
5
Return or print the resulting string.
💻

Code

ruby
puts 'Enter a sentence:'
sentence = gets.chomp
capitalized = sentence.split.map(&:capitalize).join(' ')
puts capitalized
Output
Enter a sentence: hello ruby world Hello Ruby World
🔍

Dry Run

Let's trace the input 'hello ruby world' through the code.

1

Input string

'hello ruby world'

2

Split into words

['hello', 'ruby', 'world']

3

Capitalize each word

['Hello', 'Ruby', 'World']

4

Join words back

'Hello Ruby World'

Original WordCapitalized Word
helloHello
rubyRuby
worldWorld
💡

Why This Works

Step 1: Splitting the string

The split method breaks the sentence into individual words by spaces.

Step 2: Capitalizing words

The map(&:capitalize) applies the capitalize method to each word, making the first letter uppercase.

Step 3: Joining words

The join(' ') method combines the capitalized words back into a single string separated by spaces.

🔄

Alternative Approaches

Using String#titleize from ActiveSupport
ruby
require 'active_support/core_ext/string'
puts 'hello ruby world'.titleize
This method is simpler but requires the ActiveSupport gem, which is common in Rails projects.
Using regular expression with gsub
ruby
sentence = 'hello ruby world'
capitalized = sentence.gsub(/\b\w/) { |c| c.upcase }
puts capitalized
This uses a regex to capitalize the first letter of each word without splitting.

Complexity: O(n) time, O(n) space

Time Complexity

The code processes each character once when splitting and capitalizing, so it runs in linear time relative to the string length.

Space Complexity

Extra space is used to store the list of words and the new capitalized string, so space is also linear in the input size.

Which Approach is Fastest?

The split-map-join approach is efficient and clear; regex can be slightly faster but less readable; ActiveSupport's titleize is convenient but adds dependency overhead.

ApproachTimeSpaceBest For
split.map(&:capitalize).join(' ')O(n)O(n)General use, clear and readable
ActiveSupport titleizeO(n)O(n)Rails projects with ActiveSupport
Regex gsubO(n)O(n)When avoiding splitting, but less readable
💡
Use split.map(&:capitalize).join(' ') for a quick and clear way to capitalize each word in Ruby.
⚠️
Beginners often try to use capitalize on the whole string, which only capitalizes the first letter of the entire sentence, not each word.