Facile 0 risolte 0% di accettazione

Title Case

Capitalize the first letter of every word.

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

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

Esempi

Esempio 1
Input:
text = "hello world"

Output atteso:
expected = "Hello World"
Input:
std::string text = "hello world";

Output atteso:
std::string expected = "Hello World";
Input:
string text = "hello world";

Output atteso:
string expected = "Hello World";

Spiegazione

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

Esempio 2
Input:
text = "tHE qUICK fox"

Output atteso:
expected = "The Quick Fox"
Input:
std::string text = "tHE qUICK fox";

Output atteso:
std::string expected = "The Quick Fox";
Input:
string text = "tHE qUICK fox";

Output atteso:
string expected = "The Quick Fox";

Spiegazione

Remaining letters become lowercase: "The Quick Fox".

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Split the text into words and treat each word independently.

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.