Step 1: Design Page – WordFrequency.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WordFrequency.aspx.cs" Inherits="WordFrequency" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Count Word Frequency using Dictionary - Real Time Example</title>
<style>
body {
font-family: Arial;
background-color: #f2f5fc;
margin: 50px;
}
.container {
width: 550px;
margin: auto;
background: #fff;
border-radius: 10px;
box-shadow: 0 0 10px #ccc;
padding: 25px;
}
h2 {
text-align: center;
color: #1A2A80;
}
.form-control {
width: 100%;
height: 100px;
padding: 10px;
margin-top: 10px;
resize: none;
}
.btn {
background-color: #7A85C1;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
width: 100%;
margin-top: 15px;
}
.btn:hover {
background-color: #5b68a1;
}
.result {
margin-top: 20px;
font-weight: bold;
color: #333;
white-space: pre-line;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Count Word Frequency</h2>
<asp:Label ID="Label1" runat="server" Text="Enter a sentence:"></asp:Label><br />
<asp:TextBox ID="txtInput" runat="server" CssClass="form-control" TextMode="MultiLine"
placeholder="Example: welcome to c sharp webforms welcome to coding"></asp:TextBox>
<asp:Button ID="btnCount" runat="server" Text="Count Word Frequency" CssClass="btn" OnClick="btnCount_Click" />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – WordFrequency.aspx.cs
using System;
using System.Collections.Generic;
public partial class WordFrequency : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCount_Click(object sender, EventArgs e)
{
lblResult.Text = "";
string input = txtInput.Text.Trim();
if (string.IsNullOrEmpty(input))
{
lblResult.Text = "Please enter a sentence.";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
// Convert to lowercase and split by spaces
string[] words = input.ToLower().Split(' ');
// Create dictionary to store word counts
Dictionary<string, int> wordCount = new Dictionary<string, int>();
// Count frequency
foreach (string word in words)
{
if (wordCount.ContainsKey(word))
wordCount[word]++;
else
wordCount[word] = 1;
}
// Display result
string result = "Word Frequency:\n\n";
foreach (KeyValuePair<string, int> kvp in wordCount)
{
result += kvp.Key + " → " + kvp.Value + "\n";
}
lblResult.Text = result;
lblResult.ForeColor = System.Drawing.Color.Green;
}
}
Real-Time Example Output
Input
welcome to c sharp webforms welcome to coding
Output
Word Frequency:
welcome → 2
to → 2
c → 1
sharp → 1
webforms → 1
coding → 1
Explanation
| Step | Description |
|---|
| 1 | User enters a sentence in the textbox. |
| 2 | The program converts it to lowercase to ensure case-insensitive counting. |
| 3 | The sentence is split into words using Split(' '). |
| 4 | A Dictionary<string, int> is used to store and count each word. |
| 5 | Words already present in the dictionary have their count incremented. |
| 6 | The frequency of each word is displayed in the label. |
Algorithm
1. Read sentence from user.
2. Convert sentence to lowercase.
3. Split into words by space.
4. Create an empty dictionary (wordCount).
5. For each word:
If word exists in dictionary → increment count.
Else → add word with count 1.
6. Display all words with their frequency.
Sample Test Cases
| Input | Output |
|---|
| “hello hello world” | hello → 2, world → 1 |
| “I love coding coding” | i → 1, love → 1, coding → 2 |
| “This is a test this is test” | this → 2, is → 2, a → 1, test → 2 |
Key Concepts Used
Dictionary<TKey, TValue> → Stores word and its count.
ContainsKey() → Checks if a word already exists.
foreach loop → Iterates through dictionary pairs.
Split(' ') → Splits words by space.
ToLower() → Makes counting case-insensitive.