Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of words in a text file

Tags:

c#

.net

regex

I'm trying to count the number of words from a text file, namely this, to start.

This is a test of the word count program. This is only a test. If your program works successfully, you should calculate that there are 30 words in this file.

I am using StreamReader to put everything from the file into a string, and then use the .Split method to get the number of individual words, but I keep getting the wrong value when I compile and run the program.

using System;
using System.IO;

class WordCounter
{
    static void Main()
    {
        string inFileName = null;

        Console.WriteLine("Enter the name of the file to process:");
        inFileName = Console.ReadLine();

        StreamReader sr = new StreamReader(inFileName);

        int counter = 0;
        string delim = " ,.";
        string[] fields = null;
        string line = null;

        while(!sr.EndOfStream)
        {
            line = sr.ReadLine();
        }



        fields = line.Split(delim.ToCharArray());
        for(int i = 0; i < fields.Length; i++)
        {
            counter++;
        }
        sr.Close();
        Console.WriteLine("The word count is {0}", counter);
    }
} 
like image 756
user1781027 Avatar asked Nov 05 '12 23:11

user1781027


2 Answers

Try to use regular expression, e.g.:

int count = Regex.Matches(input, @"\b\w+\b").Count;
like image 113
Kirill Polishchuk Avatar answered Oct 05 '22 22:10

Kirill Polishchuk


this should work for you:

using System;
using System.IO;

class WordCounter
{
static void Main()
{
      string inFileName = null;

      Console.WriteLine("Enter the name of the file to process:");
      inFileName = Console.ReadLine();

      StreamReader sr = new StreamReader(inFileName);

      int counter = 0;
      string delim = " ,."; //maybe some more delimiters like ?! and so on
      string[] fields = null;
      string line = null;

      while(!sr.EndOfStream)
      {
         line = sr.ReadLine();//each time you read a line you should split it into the words
         line.Trim();
         fields = line.Split(delim.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
         counter+=fields.Length; //and just add how many of them there is
      }


      sr.Close();
      Console.WriteLine("The word count is {0}", counter);
}

}

like image 21
Nikola Davidovic Avatar answered Oct 05 '22 22:10

Nikola Davidovic