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
- Split the name. Store
fullName.Split(' ')in astring[]namedwords. Index 0 is the first name and index 1 is the last name. - 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 achar. - Join the two letters. Both are strings, so
+glues them:"g" + "h"is"gh". - 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, andSubstring(0, 1)on it throws anArgumentOutOfRangeException. - 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
| fullName | Result |
|---|---|
"grace hopper" | "GH" |
"Ada Lovelace" | "AL" |
"a b" | "AB" |
