C# Program to convert first character uppercase in a sentence

Converting the first character of each word to uppercase in a sentence is a common text formatting task in C#. This process involves identifying word boundaries and converting the first lowercase letter of each word to its uppercase equivalent.

Syntax

Following is the basic approach to convert characters from lowercase to uppercase −

char uppercaseChar = (char)(lowercaseChar - 'a' + 'A');

To check if a character is a lowercase letter −

if (ch >= 'a' && ch <= 'z') {
    // Character is lowercase
}

Using Manual Character Conversion

This approach manually converts characters by manipulating their ASCII values. The first character of each word is identified and converted to uppercase −

using System;

class Demo {
    static string CapitalizeWords(string str) {
        char[] val = str.ToCharArray();
        
        for (int i = 0; i < str.Length; i++) {
            if (i == 0 && val[i] != ' ' ||
                val[i] != ' ' && val[i - 1] == ' ') {
                if (val[i] >= 'a' && val[i] <= 'z') {
                    val[i] = (char)(val[i] - 'a' + 'A');
                }
            } else if (val[i] >= 'A' && val[i] <= 'Z') {
                val[i] = (char)(val[i] + 'a' - 'A');
            }
        }
        
        return new string(val);
    }
    
    public static void Main() {
        string str = "welcome to our website!";
        Console.WriteLine("Original: " + str);
        Console.WriteLine("Capitalized: " + CapitalizeWords(str));
    }
}

The output of the above code is −

Original: welcome to our website!
Capitalized: Welcome To Our Website!

Using Built-in Methods

C# provides built-in methods that make this task simpler. The TextInfo.ToTitleCase() method can convert strings to title case −

using System;
using System.Globalization;

class Program {
    public static void Main() {
        string str = "hello world programming";
        TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
        string result = textInfo.ToTitleCase(str.ToLower());
        
        Console.WriteLine("Original: " + str);
        Console.WriteLine("Title Case: " + result);
    }
}

The output of the above code is −

Original: hello world programming
Title Case: Hello World Programming

Using String Split and Join

Another approach splits the sentence into words, capitalizes each word individually, then joins them back −

using System;
using System.Linq;

class Program {
    static string CapitalizeEachWord(string input) {
        return string.Join(" ", input.Split(' ')
            .Select(word => word.Length > 0 ? 
                char.ToUpper(word[0]) + word.Substring(1).ToLower() : word));
    }
    
    public static void Main() {
        string sentence = "c# programming is fun";
        string capitalized = CapitalizeEachWord(sentence);
        
        Console.WriteLine("Original: " + sentence);
        Console.WriteLine("Capitalized: " + capitalized);
    }
}

The output of the above code is −

Original: c# programming is fun
Capitalized: C# Programming Is Fun

Comparison of Methods

Method Performance Code Complexity Unicode Support
Manual ASCII conversion Fast Medium Limited
TextInfo.ToTitleCase() Good Low Full
Split and Join with LINQ Moderate Low Full

Conclusion

Converting the first character of each word to uppercase can be achieved through manual character manipulation, built-in .NET methods like TextInfo.ToTitleCase(), or using string operations with LINQ. The choice depends on performance requirements and Unicode support needs.

Updated on: 2026-03-17T07:04:35+05:30

558 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements