Easy 0 solved 0% acceptance

Acronym Generator

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

What you'll learn

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

Problem

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.

Constraints

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.

Examples

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

Expected output:
expected = "CPU"
Input:
std::string phrase = "central-processing unit";

Expected output:
std::string expected = "CPU";
Input:
string phrase = "central-processing unit";

Expected output:
string expected = "CPU";

Explanation

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

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

Expected output:
expected = "DRY"
Input:
std::string phrase = "don't repeat yourself";

Expected output:
std::string expected = "DRY";
Input:
string phrase = "don't repeat yourself";

Expected output:
string expected = "DRY";

Explanation

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

How to approach it

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

Hint 1

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

Hint 2

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

Hint 3

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

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.