C#  

Capitalize first letter of each word in a string

Step 1: Design Page – CapitalizeWords.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CapitalizeWords.aspx.cs" Inherits="CapitalizeWords" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Capitalize First Letter of Each Word - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            margin: 50px;
        }
        .container {
            width: 500px;
            margin: auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0px 0px 10px #ccc;
            padding: 25px;
        }
        h2 {
            color: #1A2A80;
            text-align: center;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            margin-top: 10px;
        }
        .btn {
            background-color: #7A85C1;
            color: white;
            border: none;
            padding: 10px;
            margin-top: 10px;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }
        .result {
            margin-top: 20px;
            font-weight: bold;
            color: #333;
            text-align: center;
            white-space: pre-line;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Capitalize First Letter of Each Word</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter a Sentence:"></asp:Label><br />
            <asp:TextBox ID="txtInput" runat="server" CssClass="form-control" placeholder="Example: welcome to asp.net webforms"></asp:TextBox><br />

            <asp:Button ID="btnCapitalize" runat="server" Text="Capitalize Words" CssClass="btn" OnClick="btnCapitalize_Click" /><br />

            <asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Backend Logic – CapitalizeWords.aspx.cs

using System;
using System.Globalization;

public partial class CapitalizeWords : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnCapitalize_Click(object sender, EventArgs e)
    {
        string input = txtInput.Text.Trim();

        if (string.IsNullOrEmpty(input))
        {
            lblResult.Text = "Please enter a valid sentence.";
            lblResult.ForeColor = System.Drawing.Color.Red;
            return;
        }

        // Convert to Title Case using TextInfo
        TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
        string capitalized = textInfo.ToTitleCase(input.ToLower());

        lblResult.Text = $"Converted Sentence:\n{capitalized}";
        lblResult.ForeColor = System.Drawing.Color.Green;
    }
}

Real-Time Example Flow

  1. Open CapitalizeWords.aspx in your browser.

  2. Enter:
    welcome to asp.net webforms

  3. Click “Capitalize Words”

  4. Output:

    Converted Sentence:
    Welcome To Asp.Net Webforms
    

Explanation

StepDescription
1Get user input from TextBox.
2Convert the input to lowercase for uniform formatting.
3Use TextInfo.ToTitleCase() to capitalize each word’s first letter.
4Display the final formatted string on the page.

Example Input / Output Table

InputOutput
welcome to asp.net webformsWelcome To Asp.Net Webforms
hello world from c#Hello World From C#
this is a real time exampleThis Is A Real Time Example
sandhiya v projectSandhiya V Project
learn programming easilyLearn Programming Easily

Alternate Manual Method (Without Built-in Function)

If you want to capitalize manually:

string[] words = input.Split(' ');
string result = "";

foreach (string word in words)
{
    if (word.Length > 0)
        result += char.ToUpper(word[0]) + word.Substring(1).ToLower() + " ";
}

lblResult.Text = $"Converted Sentence:\n{result.Trim()}";