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.
sentence.Split(' ')cuts the string at every space and returns astring[]."hello world"giveshelloandworld.- For each word, the first character in upper case is joined to the rest of the word.
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
- Split
sentenceat the spaces into astring[]namedwords. - Create an empty
List<string>namedresultto collect the capitalized words in order. - Write a
foreachloop with astring wordoverwords. - Build
capitalized:word.Substring(0, 1)is the first character as a one-letter string,ToUpper()makes it upper case, andword.Substring(1)is everything after it. Joining the two with+gives the word with its first letter capitalized. - Add
capitalizedtoresult. - 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. Callingword.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.Jointakes a separator and a collection of strings, so it accepts theList<string>directly.
Edge cases and common mistakes
- Lower-casing the rest. Applying
ToUpper()to the whole word, orToLower()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, andSubstring(0, 1)on an empty string throws anArgumentOutOfRangeException. That input is outside this lesson's constraints. - One-word sentences. They work:
Splitreturns 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:
| sentence | result |
|---|---|
"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" |
