Skip to content

Word & Character Counter — C# source

Live word, character, sentence, and paragraph counts plus reading-time estimate as you type.

This is the C# implementation — the same logic the interactive tool runs, in a shareable, citable form.

// word-counter — C# port: word / character / sentence / paragraph counts + 200-wpm reading-time estimate.
//
// Display snippet: ports the counters from the TypeScript lib (src/lib/wordCount.ts). C# strings are
// UTF-16, so string.Length is the same unit the TS reference counts; char.IsWhiteSpace matches the
// JS /\s/ class (both include Unicode spaces).
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace WordCounter;

public static class WordCounter
{
    // Words: split on whitespace runs, dropping empty pieces
    // (JS trim + /\s+/ + filter(Boolean)). Blank input -> 0.
    public static int CountWords(string t) =>
        t.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries).Length;

    // Total character count, whitespace included.
    public static int CountChars(string t) => t.Length;

    // Character count with all whitespace removed.
    public static int CountCharsNoSpaces(string t) =>
        t.Count(c => !char.IsWhiteSpace(c));

    // Sentences: maximal runs of non-terminators each closed by one or more
    // of . ! ? — the TS regex /[^.!?]+[.!?]+/g.
    public static int CountSentences(string t) =>
        Regex.Matches(t, @"[^.!?]+[.!?]+").Count;

    // Paragraphs separated by one or more blank lines; blank segments don't count.
    public static int CountParagraphs(string t) =>
        Regex.Split(t, @"\n{2,}").Count(s => !string.IsNullOrWhiteSpace(s));

    // Reading time at 200 wpm: 0 for empty input, otherwise >= 1 minute.
    // (words + 100) / 200 reproduces Math.round(words / 200.0) with
    // away-from-zero halves.
    public static long ReadingTimeMin(long words) =>
        words <= 0 ? 0 : Math.Max(1, (words + 100) / 200);
}

public static class Demo
{
    public static void Main()
    {
        const string text = "CosmoDev counts words. Live totals update as you type!\n\n"
            + "Second paragraph? Two sentences - each with words. Zero logins needed.";
        Console.WriteLine($"words:       {WordCounter.CountWords(text)}");          // 20
        Console.WriteLine($"chars:       {WordCounter.CountChars(text)}");          // 126
        Console.WriteLine($"no spaces:   {WordCounter.CountCharsNoSpaces(text)}");  // 106
        Console.WriteLine($"sentences:   {WordCounter.CountSentences(text)}");      // 5
        Console.WriteLine($"paragraphs:  {WordCounter.CountParagraphs(text)}");     // 2
        Console.WriteLine($"reading min: {WordCounter.ReadingTimeMin(WordCounter.CountWords(text))}"); // 1
    }
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →