0
0
CsharpProgramBeginner · 2 min read

C# Program to Capitalize First Letter of Each Word

Use System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string.ToLower()) to capitalize the first letter of each word in C#.
📋

Examples

Inputhello world
OutputHello World
Inputc# programming language
OutputC# Programming Language
Input123 easy as abc
Output123 Easy As Abc
🧠

How to Think About It

To capitalize the first letter of each word, first convert the entire string to lowercase to standardize it, then change the first letter of each word to uppercase. This ensures consistent capitalization even if the input has mixed cases.
📐

Algorithm

1
Get the input string from the user.
2
Convert the entire string to lowercase to normalize the text.
3
Use a built-in method to capitalize the first letter of each word.
4
Return or print the transformed string.
💻

Code

csharp
using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        string input = "hello world from c#";
        string result = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input.ToLower());
        Console.WriteLine(result);
    }
}
Output
Hello World From C#
🔍

Dry Run

Let's trace the input "hello world from c#" through the code.

1

Input string

input = "hello world from c#"

2

Convert to lowercase

input.ToLower() = "hello world from c#" (already lowercase)

3

Capitalize first letter of each word

ToTitleCase("hello world from c#") = "Hello World From C#"

4

Print result

Output: "Hello World From C#"

StepOperationResult
1Input stringhello world from c#
2ToLower()hello world from c#
3ToTitleCase()Hello World From C#
4Print outputHello World From C#
💡

Why This Works

Step 1: Normalize case

Converting the string to lowercase ensures that all letters start from a consistent state before capitalizing.

Step 2: Capitalize words

The ToTitleCase method changes the first letter of each word to uppercase automatically.

Step 3: Output result

The final string has each word's first letter capitalized, making it look neat and readable.

🔄

Alternative Approaches

Manual split and capitalize
csharp
using System;
class Program
{
    static void Main()
    {
        string input = "hello world from c#";
        string[] words = input.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            if (words[i].Length > 0)
                words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1).ToLower();
        }
        string result = string.Join(" ", words);
        Console.WriteLine(result);
    }
}
This approach gives more control but requires more code and manual handling.
Using Regex replacement
csharp
using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main()
    {
        string input = "hello world from c#";
        string result = Regex.Replace(input.ToLower(), "\\b[a-z]", m => m.Value.ToUpper());
        Console.WriteLine(result);
    }
}
Uses regular expressions to find word starts and capitalize them; good for custom patterns but less readable.

Complexity: O(n) time, O(n) space

Time Complexity

The program processes each character once when converting case and capitalizing, so it runs in linear time relative to string length.

Space Complexity

A new string is created for the output, so space grows linearly with input size.

Which Approach is Fastest?

Using ToTitleCase is efficient and concise; manual splitting or regex adds overhead and complexity.

ApproachTimeSpaceBest For
ToTitleCase methodO(n)O(n)Simple and fast capitalization
Manual split and capitalizeO(n)O(n)More control over each word
Regex replacementO(n)O(n)Flexible pattern matching
💡
Use ToTitleCase on a lowercase string to reliably capitalize each word's first letter.
⚠️
Not converting the string to lowercase first can cause inconsistent capitalization if input has mixed cases.