Tutte le sfide
Facile 0 risolte 0% di accettazione

Acronym Generator

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

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

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.

Esempi

Esempio 1
Input:
phrase = "central-processing unit"

Output atteso:
expected = "CPU"
Input:
std::string phrase = "central-processing unit";

Output atteso:
std::string expected = "CPU";
Input:
string phrase = "central-processing unit";

Output atteso:
string expected = "CPU";

Spiegazione

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

Esempio 2
Input:
phrase = "don't repeat yourself"

Output atteso:
expected = "DRY"
Input:
std::string phrase = "don't repeat yourself";

Output atteso:
std::string expected = "DRY";
Input:
string phrase = "don't repeat yourself";

Output atteso:
string expected = "DRY";

Spiegazione

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

Come affrontarlo

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

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

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

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

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