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
Open CapitalizeWords.aspx in your browser.
Enter:
welcome to asp.net webforms
Click “Capitalize Words”
Output:
Converted Sentence:
Welcome To Asp.Net Webforms
Explanation
| Step | Description |
|---|
| 1 | Get user input from TextBox. |
| 2 | Convert the input to lowercase for uniform formatting. |
| 3 | Use TextInfo.ToTitleCase() to capitalize each word’s first letter. |
| 4 | Display the final formatted string on the page. |
Example Input / Output Table
| Input | Output |
|---|
| welcome to asp.net webforms | Welcome To Asp.Net Webforms |
| hello world from c# | Hello World From C# |
| this is a real time example | This Is A Real Time Example |
| sandhiya v project | Sandhiya V Project |
| learn programming easily | Learn 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()}";