Start learning free

LESSON 220 OF 289 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Capitalize Each Word in C#: Split, ToUpper and Join

Practice Read the full explanation

Given a string sentence, return the same sentence with the first letter of each word in upper case.

The words are separated by single spaces, and the sentence doesn't start or end with a space. Only the first character of each word changes: the rest of the word stays exactly as it is, even when it has upper-case letters. A first character that has no upper case, like a digit, stays as it is too.

 

Example 1:

Input: sentence = "hello world"
Output: "Hello World"
Explanation: h becomes H and w becomes W.

Example 2:

Input: sentence = "learn to code with AlgoCademy"
Output: "Learn To Code With AlgoCademy"
Explanation: AlgoCademy already starts with an upper-case letter, so it stays the same.

Example 3:

Input: sentence = "iPhone and NASA"
Output: "IPhone And NASA"
Explanation: Only the first character of each word changes: "Phone" and "ASA" stay as they were.

 

Constraints:

  • sentence has at least one word.
  • sentence contains English letters, digits and apostrophes, with single spaces between the words.
STEP 1 OF 6
    Ctrl + Enter to check

    C# · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Capitalize Each Word in C#, explained

    Problem overview

    CapitalizeWords receives a sentence and returns it with the first letter of each word in upper case. Words are separated by single spaces, and the sentence doesn't start or end with a space. Only the first character of each word changes. The rest stays exactly as it is, even when it contains upper-case letters, and a first character with no upper case, such as a digit, stays too.

    "hello world" becomes "Hello World". "learn to code with AlgoCademy" becomes "Learn To Code With AlgoCademy", since AlgoCademy already starts with a capital. "iPhone and NASA" becomes "IPhone And NASA": only the first character of each word is touched.

    Approach

    Break the problem into three pieces: cut the sentence into words, fix each word, and glue the words back together.

    1. sentence.Split(' ') cuts the string at every space and returns a string[]. "hello world" gives hello and world.
    2. For each word, the first character in upper case is joined to the rest of the word.
    3. string.Join(" ", result) puts one space between the items of a list and returns one string.

    Because the words are separated by single spaces, splitting on a space and joining with a space restores every space exactly where it was.

    Step by step

    1. Split sentence at the spaces into a string[] named words.
    2. Create an empty List<string> named result to collect the capitalized words in order.
    3. Write a foreach loop with a string word over words.
    4. Build capitalized: word.Substring(0, 1) is the first character as a one-letter string, ToUpper() makes it upper case, and word.Substring(1) is everything after it. Joining the two with + gives the word with its first letter capitalized.
    5. Add capitalized to result.
    6. Return string.Join(" ", result) after the loop.

    The solution in C#

    class Solution {
        public string CapitalizeWords(string sentence) {
            string[] words = sentence.Split(' ');
            List<string> result = new List<string>();
            foreach (string word in words) {
                string capitalized = word.Substring(0, 1).ToUpper() + word.Substring(1);
                result.Add(capitalized);
            }
            return string.Join(" ", result);
        }
    }

    Details that matter

    • Strings are immutable. ToUpper() doesn't change the string it is called on: it returns a new one. Calling word.ToUpper(); on its own line and ignoring the result does nothing, which is why the solution builds a new string and stores it.
    • Substring(1) on a one-letter word returns an empty string, not an error, because the start index equals the string's length. So "a" becomes "A".
    • Only the first character is changed. The rest is copied as it is, so "NASA" stays "NASA" and "iPhone" becomes "IPhone".
    • Digits stay put. "1st" becomes "1st", because a digit has no upper-case form.
    • string.Join takes a separator and a collection of strings, so it accepts the List<string> directly.

    Edge cases and common mistakes

    • Lower-casing the rest. Applying ToUpper() to the whole word, or ToLower() to the rest, would change "NASA" or "AlgoCademy".
    • Double spaces. The lesson promises single spaces. With two spaces in a row, Split(' ') produces an empty string between them, and Substring(0, 1) on an empty string throws an ArgumentOutOfRangeException. That input is outside this lesson's constraints.
    • One-word sentences. They work: Split returns an array of one item.
    • Forgetting to use the result. Keep the capitalized word in result, or the original words come back unchanged.

    Testing

    The solution returns these results:

    sentenceresult
    "hello world""Hello World"
    "learn to code with AlgoCademy""Learn To Code With AlgoCademy"
    "iPhone and NASA""IPhone And NASA"
    "a b""A B"
    "1st place""1st Place"
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free