Convert words to title case
Convert a space-separated phrase to title case by uppercasing each word's first letter and lowercasing its remaining letters.
Course progress
Problem 58 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
text = "bUILD useful TOOLS"
Expected output
Build Useful Tools
What is happening?
Uppercase the first letter of each word and lowercase its remaining letters to produce Build Useful Tools.
Solution strategy
Build the right mental model
Scan a character array while tracking word boundaries. Uppercase a letter after whitespace and lowercase other letters.
Core concepts
strings · character conversion
Complexity
Time O(n), space O(n).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for title-case-words
using System;
class Program
{
static string ToTitleCase(string text)
{
char[] characters = text.ToCharArray();
bool startsWord = true;
for (int i = 0; i < characters.Length; i++)
{
if (char.IsWhiteSpace(characters[i])) { startsWord = true; continue; }
characters[i] = startsWord
? char.ToUpperInvariant(characters[i])
: char.ToLowerInvariant(characters[i]);
startsWord = false;
}
return new string(characters);
}
static void Main()
{
Console.WriteLine(ToTitleCase("bUILD useful TOOLS"));
}
}Knowledge check
Questions students often ask
What is the main idea behind convert words to title case?
Scan a character array while tracking word boundaries. Uppercase a letter after whitespace and lowercase other letters.
What is the time and space complexity of this solution?
Time O(n), space O(n).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.