Tous les défis
Facile 0 résolus 0 % de réussite

Acronym Generator

Create an acronym from the first letter of every word, in uppercase.

Ce que vous allez apprendre

  • Split text on spaces and hyphens
  • Take the first letter of each word
  • Convert letters to uppercase

Problème

Afficher la traduction

Create an acronym from the first letter of every word, in uppercase. Spaces and hyphens separate words. Remove every other punctuation mark and digit without adding a separator; an apostrophe inside a word does not start another word. Ignore empty pieces.

Implement acronym_generator(phrase).

Input

phrase: The phrase to shorten.

Output

Return the uppercase initials in their original order.

Contraintes

0 ≤ phrase length ≤ 10,000. Input contains ASCII letters, digits, spaces, and punctuation; no tabs or line breaks. A phrase with no letters returns empty text.

Exemples

Exemple 1
Entrée :
phrase = "central-processing unit"

Sortie attendue :
expected = "CPU"
Entrée :
std::string phrase = "central-processing unit";

Sortie attendue :
std::string expected = "CPU";
Entrée :
string phrase = "central-processing unit";

Sortie attendue :
string expected = "CPU";

Explication

The hyphen and space separate central, processing, and unit. Their initials are C, P, and U.

Exemple 2
Entrée :
phrase = "don't repeat yourself"

Sortie attendue :
expected = "DRY"
Entrée :
std::string phrase = "don't repeat yourself";

Sortie attendue :
std::string expected = "DRY";
Entrée :
string phrase = "don't repeat yourself";

Sortie attendue :
string expected = "DRY";

Explication

Remove the apostrophe to obtain dont. It is still one word, followed by repeat and yourself.

Comment l'aborder

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

Indice 1

Treat spaces and hyphens as word separators, but ignore digits and other punctuation within or around words.

Indice 2

Look for the first letter after each separator and uppercase it.

Indice 3

Apostrophes inside a word should not create a new word, and empty chunks should be skipped.

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.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

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