Step 1: Design Page – CharacterFrequency.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CharacterFrequency.aspx.cs" Inherits="CharacterFrequency" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Character Frequency in a String - 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>Find Frequency of Each Character</h2>
<asp:Label ID="lblInput" runat="server" Text="Enter a String:"></asp:Label><br />
<asp:TextBox ID="txtInput" runat="server" CssClass="form-control" placeholder="Example: programming"></asp:TextBox><br />
<asp:Button ID="btnFind" runat="server" Text="Find Frequency" CssClass="btn" OnClick="btnFind_Click" /><br />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – CharacterFrequency.aspx.cs
using System;
using System.Collections.Generic;
public partial class CharacterFrequency : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFind_Click(object sender, EventArgs e)
{
string input = txtInput.Text.Trim().ToLower();
if (string.IsNullOrEmpty(input))
{
lblResult.Text = "Please enter a valid string.";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
Dictionary<char, int> charCount = new Dictionary<char, int>();
// Count frequency of each character
foreach (char c in input)
{
if (char.IsLetterOrDigit(c)) // count only letters and digits
{
if (charCount.ContainsKey(c))
charCount[c]++;
else
charCount[c] = 1;
}
}
// Display the result
string result = "Character Frequency:\n";
foreach (var item in charCount)
{
result += $"'{item.Key}' = {item.Value}\n";
}
lblResult.Text = result;
lblResult.ForeColor = System.Drawing.Color.DarkBlue;
}
}
Real-Time Example Flow
Open CharacterFrequency.aspx in your browser.
Enter: programming
Click “Find Frequency”
Output will be displayed as:
Character Frequency:
'p' = 1
'r' = 2
'o' = 1
'g' = 2
'a' = 1
'm' = 2
'i' = 1
'n' = 1
Explanation
| Step | Description |
|---|
| 1 | Get the user input string from the TextBox. |
| 2 | Convert to lowercase to make counting case-insensitive. |
| 3 | Use a Dictionary to count each character’s frequency. |
| 4 | Display all character counts in a readable format. |
Example Input / Output Table
| Input | Output |
|---|
| programming | p=1, r=2, o=1, g=2, a=1, m=2, i=1, n=1 |
| hello | h=1, e=1, l=2, o=1 |
| success | s=3, u=1, c=2, e=1 |
| webforms | w=1, e=1, b=1, f=1, o=1, r=1, m=1, s=1 |
| sandhiya | s=1, a=2, n=1, d=1, h=1, i=1, y=1 |