Start learning free

LESSON 74 OF 289 · STRINGS - CHALLENGES

Initials in C#: Split, Substring and ToUpper

Practice Read the full explanation

Given a person's full name fullName, made of a first name and a last name, return their initials: the first letter of each name, in upper case.

The two names are separated by a single space. The initials are always upper case, even when the name is written in lower case.

 

Example 1:

Input: fullName = "grace hopper"
Output: "GH"
Explanation: The first name starts with g and the last name with h. In upper case, they give "GH".

Example 2:

Input: fullName = "Ada Lovelace"
Output: "AL"
Explanation: A and L are already upper case, so they stay as they are.

Example 3:

Input: fullName = "a b"
Output: "AB"
Explanation: A name can be a single letter: that letter is its initial.

 

Constraints:

  • fullName has exactly two words, the first name and the last name, separated by one space.
  • Both names are made of English letters only, lower case or upper case.
STEP 1 OF 2
    Ctrl + Enter to check

    C# · STRINGS - CHALLENGES

    Initials in C#, explained

    Problem overview

    The method Initials receives a full name made of a first name and a last name, separated by one space, and returns the initials: the first letter of each name, in upper case. "grace hopper" gives "GH", even though the name is written in lower case. "Ada Lovelace" gives "AL". A name can be a single letter, so "a b" gives "AB".

    Approach: split, take two letters, change the case

    The work divides into three small jobs. Cut the full name into its two words. Take the first letter of each word. Turn the two letters into upper case.

    Split(' ') cuts a string at every space and returns a string[]. With exactly one space in the name, the result has exactly two items:

    string[] words = "grace hopper".Split(' ');
    Console.WriteLine(words.Length);
    Console.WriteLine(words[0] + "|" + words[1]);
    2
    grace|hopper

    Step by step

    1. Split the name. Store fullName.Split(' ') in a string[] named words. Index 0 is the first name and index 1 is the last name.
    2. Take the first letter of each word as a string. words[0].Substring(0, 1) starts at index 0 and takes 1 character. The result is a string, not a char.
    3. Join the two letters. Both are strings, so + glues them: "g" + "h" is "gh".
    4. Change the case. Put the sum in parentheses and call ToUpper() on it. The parentheses matter: without them, ToUpper() would apply only to the second letter.

    The solution in C#

    class Solution {
        public string Initials(string fullName) {
            string[] words = fullName.Split(' ');
            return (words[0].Substring(0, 1) + words[1].Substring(0, 1)).ToUpper();
        }
    }

    Strings are immutable in C#, so ToUpper() does not change anything in place: it returns a new string. That is why the call is part of the returned expression. Letters that are already upper case stay as they are, which is why "Ada Lovelace" works with the same code as "grace hopper".

    Why Substring and not an index

    The indexer words[0][0] also gives the first letter, but as a char. Two char values with + between them are added as numbers, so the result is a number, not text:

    string[] words = "grace hopper".Split(' ');
    Console.WriteLine(words[0][0] + words[1][0]);
    Console.WriteLine("" + char.ToUpper(words[0][0]) + char.ToUpper(words[1][0]));
    207
    GH

    The second line shows the way to keep chars: start from an empty string, and convert each one with char.ToUpper. Working with Substring(0, 1) avoids the problem, because the letters are strings from the start.

    Edge cases and common mistakes

    • Calling ToUpper() on the wrong thing. words[0].Substring(0, 1) + words[1].Substring(0, 1).ToUpper() would leave the first initial as it was written: "gH" for "grace hopper".
    • Single-letter names. "a b" works: Substring(0, 1) on a one-letter word returns that letter.
    • More than one space. The lesson promises exactly one. With two spaces in a row, Split(' ') produces an empty string between them, and Substring(0, 1) on it throws an ArgumentOutOfRangeException.
    • Index 2. The words are numbered 0 and 1. A name with only two words has no words[2].

    The method has no loop and no condition: it relies on the lesson's promise that the name has exactly two words. That keeps the code short, and it also means the structure of the input decides how safe the code is. When the input format is promised, direct indexes such as words[1] are the simplest tool; when it is not, you would check words.Length first.

    Testing

    fullNameResult
    "grace hopper""GH"
    "Ada Lovelace""AL"
    "a b""AB"
    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