Skip to the content

File names

Problem

For years, the photo folder saved names with underscores, like trip_to_the_beach. The new app wants them joined together, with a capital at the start of every word except the first: tripToTheBeach. Write a function that takes name, a text of lowercase letters and underscores, and returns the new name as a text. A word is each stretch of letters between underscores. All the underscores go away, and the first letter of each word becomes a capital, except for the first word, which stays in lowercase. The other letters don't change.

Repeated underscores, and the ones at the start or at the end, don't make words: they are just removed. So _walk__by_the_lake_ gives walkByTheLake. If the text is empty or has only underscores, you return an empty text.

Examples

  • The one in the example

    "trip_to_the_beach" → "tripToTheBeach"

  • A single word

    "cat" → "cat"

  • Two words

    "my_dog" → "myDog"

  • Two underscores in a row

    "birthday__for_ana" → "birthdayForAna"

  • An empty text

    "" → ""

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def to_camel(name):
    pass

JavaScript

function toCamel(name) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →