Skip to content

Ozar.net Developer Blog

Programming in PHP, C, C#, ASP.NET, Java, Objective-C, SQL also for MS SQL Server, Oracle & MySQL Development

Menu
  • Home
  • About
  • Tutorials
  • Contact
  • Privacy
Menu

How to count the words in a String in C#

Posted on April 15, 2010April 15, 2010 by OD

Remember the Word Count function in Microsoft Word? Here’s how we could implement counting the words in a String in C#:

The following is the spource code of a program featuring two word counting methods, both of which yield fairly similar results to Microsoft Word from Microsoft Office 2007. The example program below first executes a Regex-based function for counting the words in the string constant, and then executes the loop-based one.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        const string t1 = "To be or not to be, that is the question.";
        Console.WriteLine(WordCounting.CountWords1(t1));
        Console.WriteLine(WordCounting.CountWords2(t1));

        const string t2 = "Mary had a little lamb.";
        Console.WriteLine(WordCounting.CountWords1(t2));
        Console.WriteLine(WordCounting.CountWords2(t2));
    }
}

/// 
/// Contains methods for counting words.
/// 
public static class WordCounting
{
    /// 
    /// Count words with Regex.
    /// 
    public static int CountWords1(string s)
    {
        MatchCollection collection = Regex.Matches(s, @"[\S]+");
        return collection.Count;
    }

    /// 
    /// Count word with loop and character tests.
    /// 
    public static int CountWords2(string s)
    {
        int c = 0;
        for (int i = 1; i < s.Length; i++)         {             if (char.IsWhiteSpace(s[i - 1]) == true)             {                 if (char.IsLetterOrDigit(s[i]) == true ||                     char.IsPunctuation(s[i]))                 {                     c++;                 }             }         }         if (s.Length > 2)
        {
            c++;
        }
        return c;
    }
}

Program Output

10
10
5
5

It has static methods. This code is ideally contained in static methods as  it doesn’t maintain state or any data. You can think of it as an action, not an object. The methods each receive a string. Both approaches above receive a string and return an integer equal to the number of words they calculate.

The first method uses Regex. The first method here, CountWords1, is better in every way except perhaps performance. It is shorter and simpler to maintain, and is also considerably more accurate. The backslash-S characters (\S) mean characters that are not spaces. So the first method considers each non-letter character to be part of a word, similar to Microsoft Word.

Source: http://dotnetperls.com/word-count

2 thoughts on “How to count the words in a String in C#”

  1. Erin says:
    April 15, 2010 at 11:28 PM

    I need a shorter and practical function to count the words delimited by spaces in a string.

    For example: in string like “ab cd de” the function should return 3.

    Any suggestions?

    Log in to Reply
    1. OD says:
      April 15, 2010 at 11:50 PM

      Try the following:


      string s="ab cd de";
      string [] words=s.Split(null);
      int i=words.Length;

      alternatively you could use regex (which would also allow you to change the delimeter later )


      string input = "ab cd de";
      int result = Regex.Matches(input, " ").Count + 1;

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • NSTableView with Core Data Tutorial using NSFetchedResultsController
  • CodeIgniter Wizard – a new PHP CRUD Code Generator for macOS
  • Installing Oracle Database 10g Express Edition and Changing the Default HTTP Port
  • Xcode Tutorial 2 – Putting iAd into your App (The Apple Approved Way)
  • Cocoa Programming: A Quick-Start Guide for Developers

Blogroll

  • All Systems GO
  • GOZmosis
  • Maclord's Blog
  • Ozar.net

Tags

.NET 2008 application ASP.NET basic Beginning best books business Business Objects C# Database Databases Development Edition Guide iPhone Java Java EE learn Learning Lesson Microsoft MS SQL Server MySQL Objective-C Programming objects Oracle Part PHP PL/SQL Professional Programming Server Software SQL T-SQL TSQL Tutorial Tutorials Using Video Visual Visual Basic .NET Web Programming Tutorials
© 2026 Ozar.net Developer Blog | Powered by Minimalist Blog WordPress Theme

Powered by
►
Necessary cookies enable essential site features like secure log-ins and consent preference adjustments. They do not store personal data.
None
►
Functional cookies support features like content sharing on social media, collecting feedback, and enabling third-party tools.
None
►
Analytical cookies track visitor interactions, providing insights on metrics like visitor count, bounce rate, and traffic sources.
None
►
Advertisement cookies deliver personalized ads based on your previous visits and analyze the effectiveness of ad campaigns.
None
►
Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies.
None
Powered by