C#  

Count words in a sentence

Step 1: Design Page – CountWords.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Count Words in a Sentence - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            margin: 50px;
        }
        .container {
            width: 480px;
            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>Count Words in a Sentence</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter a Sentence:"></asp:Label><br />
            <asp:TextBox ID="txtSentence" runat="server" CssClass="form-control" placeholder="Example: Welcome to ASP.NET WebForms"></asp:TextBox><br />

            <asp:Button ID="btnCount" runat="server" Text="Count Words" CssClass="btn" OnClick="btnCount_Click" /><br />

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

Step 2: Backend Logic – CountWords.aspx.cs

using System;

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

    protected void btnCount_Click(object sender, EventArgs e)
    {
        string sentence = txtSentence.Text.Trim();

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

        // Split words by spaces, removing empty entries
        string[] words = sentence.Split(new char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        int wordCount = words.Length;

        lblResult.Text = $"Total Words: {wordCount}";
        lblResult.ForeColor = System.Drawing.Color.Green;
    }
}

Real-Time Example Flow

  1. Open CountWords.aspx in your browser.

  2. Enter:
    Welcome to ASP.NET WebForms

  3. Click “Count Words”.

  4. Output:

    Total Words: 4
    

Explanation

StepDescription
1Get the sentence from the TextBox.
2Use Split() to divide by space, tab, or newline.
3Use StringSplitOptions.RemoveEmptyEntries to ignore multiple spaces.
4Count and display the number of words.

Example Input / Output Table

InputOutput
Welcome to ASP.NET WebForms4
I love coding in C#5
Learn ASP.NET easily3
Hello1
This is a real time example6