Easy 0 solved 0% acceptance

Title Case

Capitalize the first letter of every word.

What you'll learn

  • Change the case of single characters
  • Process each word of a sentence
  • Join words back into one string

Problem

Convert text to title case: the first letter of every word becomes uppercase and the remaining letters lowercase. Words are separated by single spaces.

Implement title_case(text).

Input

text: Lowercase or mixed-case words separated by single spaces.

Output

Return the title-cased text.

Constraints

0 ≤ length ≤ 10,000 (letters and spaces)

Examples

Example 1
Input:
text = "hello world"

Expected output:
expected = "Hello World"
Input:
std::string text = "hello world";

Expected output:
std::string expected = "Hello World";
Input:
string text = "hello world";

Expected output:
string expected = "Hello World";

Explanation

"hello" → "Hello" and "world" → "World".

Example 2
Input:
text = "tHE qUICK fox"

Expected output:
expected = "The Quick Fox"
Input:
std::string text = "tHE qUICK fox";

Expected output:
std::string expected = "The Quick Fox";
Input:
string text = "tHE qUICK fox";

Expected output:
string expected = "The Quick Fox";

Explanation

Remaining letters become lowercase: "The Quick Fox".

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Split the text into words and treat each word independently.

Hint 2

Uppercase the first character and lowercase the rest, then join the words back with spaces.

Hint 3

Watch for empty input so you do not try to access a missing first character.

Expected complexity

O(n) time, O(n) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.