Step 1: Design Page – CountVowelsConsonants.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CountVowelsConsonants.aspx.cs" Inherits="CountVowelsConsonants" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Count Vowels and Consonants - Real Time Example</title>
<style>
body {
font-family: Arial;
background-color: #f0f2f5;
margin: 50px;
}
.container {
width: 480px;
margin: auto;
background: white;
border-radius: 10px;
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;
}
.highlight {
color: #1A2A80;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Count Vowels and Consonants</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: Sandhiya Priya"></asp:TextBox><br />
<asp:Button ID="btnCount" runat="server" Text="Count Letters" CssClass="btn" OnClick="btnCount_Click" /><br />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – CountVowelsConsonants.aspx.cs
using System;
public partial class CountVowelsConsonants : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCount_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;
}
int vowelCount = 0;
int consonantCount = 0;
foreach (char c in input)
{
if (char.IsLetter(c))
{
if ("aeiou".Contains(c))
vowelCount++;
else
consonantCount++;
}
}
lblResult.Text = $"Vowels: <span class='highlight'>{vowelCount}</span> | Consonants: <span class='highlight'>{consonantCount}</span>";
lblResult.ForeColor = System.Drawing.Color.Black;
}
}
Real-Time Example Flow
Open CountVowelsConsonants.aspx in your browser.
Enter: Sandhiya Priya
Click “Count Letters”
Output displays:
Vowels: 5 | Consonants: 8
Explanation
| Step | Description |
|---|
| 1 | Accept input string from the user. |
| 2 | Convert to lowercase for uniform comparison. |
| 3 | Loop through each character. |
| 4 | Check if it’s a letter using char.IsLetter(). |
| 5 | If it’s a, e, i, o, u → vowel; else consonant. |
| 6 | Display counts dynamically. |
Sample Input / Output
| Input | Vowels | Consonants |
|---|
| Sandhiya | 3 | 5 |
| Priya | 2 | 3 |
| Welcome | 3 | 4 |
| Education | 5 | 4 |