Facile 0 résolus 0 % de réussite

Title Case

Capitalize the first letter of every word.

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

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

Exemples

Exemple 1
Entrée :
text = "hello world"

Sortie attendue :
expected = "Hello World"
Entrée :
std::string text = "hello world";

Sortie attendue :
std::string expected = "Hello World";
Entrée :
string text = "hello world";

Sortie attendue :
string expected = "Hello World";

Explication

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

Exemple 2
Entrée :
text = "tHE qUICK fox"

Sortie attendue :
expected = "The Quick Fox"
Entrée :
std::string text = "tHE qUICK fox";

Sortie attendue :
std::string expected = "The Quick Fox";
Entrée :
string text = "tHE qUICK fox";

Sortie attendue :
string expected = "The Quick Fox";

Explication

Remaining letters become lowercase: "The Quick Fox".

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Split the text into words and treat each word independently.

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.