Skip to main content
All coding problems

Title Case a String

EasystringsString traversalstringseasyformatting

Maintained by Vikas D, software engineer

You are given a string `s` made of English letters and single-space separators. The goal is to normalize its capitalization into title case.

For each word, the first character becomes uppercase and the rest of the word becomes lowercase, no matter what casing the input used. So `iPHONE` turns into `Iphone` and `the` turns into `The`.

Words are split by exactly one space, and that spacing is preserved in the output. Only the letter casing changes; the word boundaries stay where they were.

Return the transformed string.

Examples

Input: s = "hello world"
Output: "Hello World"

Each word's first letter is capitalized and the remaining letters stay lowercase.

Input: s = "I LOVE CODING"
Output: "I Love Coding"

The all-caps input is rewritten so only the leading letter of each word stays uppercase.

Input: s = "already Title Case"
Output: "Already Title Case"

Already correct words are left looking the same, and a mid-string capital like the start of a word is preserved while inner letters are lowercased.

Constraints

  • 1 <= s.length <= 10^4
  • s contains only lowercase and uppercase English letters and spaces.
  • Words are separated by exactly one space.
  • There are no leading or trailing spaces and no empty words.

Visible test cases

  • titleCase("hello world") == "Hello World"
  • titleCase("the quick brown fox") == "The Quick Brown Fox"
  • titleCase("I LOVE CODING") == "I Love Coding"
  • titleCase("singleword") == "Singleword"
  • titleCase("already Title Case") == "Already Title Case"

Hints

Hint 1

Title case is a per-word transformation. Can you split the string on spaces and handle each word on its own?

Hint 2

For one word, you need two pieces: the first character uppercased and everything after it lowercased. Slicing the word at index 1 gives you both halves.

Hint 3

Process the words, then join them back together with a single space so the original spacing is restored.

Loading the editor...

Approaches

Split, map, join

Time O(n) · Space O(n)

Treat the string as a list of words. Transform each word independently, then stitch the results back with spaces.

  1. Split `s` on the single-space separator to get an array of words.
  2. For each word, take the first character and uppercase it.
  3. Take the rest of the word (from index 1 onward) and lowercase it.
  4. Concatenate those two parts to form the title-cased word.
  5. Join all transformed words with a single space and return the result.

Single pass with a boundary flag

Time O(n) · Space O(n)

Scan the string once, tracking whether the current character is the first letter of a word. Capitalize it if so, lowercase it otherwise, and reset the flag at each space.

  1. Keep a boolean `atWordStart`, initialized to true.
  2. Walk each character left to right while building an output buffer.
  3. If the character is a space, append it and set `atWordStart` to true.
  4. Otherwise append the uppercase form when `atWordStart` is true and the lowercase form otherwise, then set `atWordStart` to false.
  5. Return the joined buffer.

Common mistakes

  • Forgetting to lowercase the rest of the word. An input like "I LOVE CODING" stays wrong if you only capitalize the first letter and leave "OVE" untouched.
  • Assuming the input is already lowercase. A mixed-case word such as "iPHONE" must be fully normalized, not just have its first letter capped.
  • Splitting on the wrong separator or collapsing spaces. Use the exact single-space split so the original word count and spacing are preserved.
  • Mutating the first character in place in languages where strings are immutable, instead of building a new string from the two parts.

Follow-up variations

  • What if words can be separated by multiple spaces or by tabs and newlines? You would need to preserve runs of whitespace rather than a single space.
  • How would you keep small words like "of", "the", and "and" lowercase unless they start the string, as in editorial headline style?
  • Could you do the transformation in place if the string were given as a mutable character array, using O(1) extra space?
  • How would this change for Unicode text where casing is locale-dependent, for example the Turkish dotted and dotless i?

Build the rest of your prep