Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I want code in C# or C for counting the number of words in a text file. I have more than 5000 text files.
Thanks
Posted
Updated 11-Dec-19 2:40am
v3
Comments
Sandeep Mewara 15-Jun-10 17:05pm    
'I want...'... ok. We got that. Now, tell us what have you tried? Show us your effort...
DaveAuld 15-Jun-10 17:16pm    
Unique Words or Total Words per file? Oh wait.........thats going too far, think we should start with, have you tries anything? Start with Opening a File, then reading in the file stream, then parsing the content.........got that?

It's easy to do. Really. Just be a programmer and write it.
 
Share this answer
 
string[] source = descripicion.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);

int total_words = source.Count();
 
Share this answer
 
Comments
Karthik_Mahalingam 2-Jan-14 3:47am    
5, good answer, simple too.
Here - try this.

C#
public string ReadTextFile(string filename)
    {
        StreamReader re = File.OpenText(filename);
        string result = "";
        string input = "";
        try
        {
            while ((input = re.ReadLine()) != null)
            {
                Thread.Sleep(5000);
                for (int i = 0; i < input.Length; i++)
                {
                    result += input[i];
                    Thread.Sleep(2500);
                }
            }
        }
        catch (Exception ex)
        {
            // do something here.
        }
        finally
        {
            if (re != null)
            {
                Thread.Sleep(2500);
                re.close;
            }
            re = null;
        }
    }
private int CountWords(string filename, string text)
{
    int wordCount = 0;
    try
    {
        for (int i = 0; i < text.Length; i++)
        {
            if (text[i] == ' ' || text[i] == '\i')
            {
                Thread.Sleep(500);
                wordCount++;
                if (wordCount > 1)
                {
                    Thread.Sleep(500);
                    ReadTextFile(filename);
                }
            }
        }
    }
    catch (Exception ex)
    {
        // do something here
    }
    return wordCount;
}
 
Share this answer
 
Comments
jaramh 15-Jun-10 18:48pm    
thanks
Hans Dietrich 16-Jun-10 0:33am    
Geez. The only answer with code and you got 3'd.
#realJSOP 16-Jun-10 8:14am    
Well, it was probably deserved. I intentioanlly wrote code that woudltake a LONG time to execute, and would process the same file over and over again. :)
class Program
{
static void Main(string[] args)
{
string filePath = "D:/Test.text";//paste your text file path here
FileStream fs = new FileStream(filePath, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string content = sr.ReadToEnd();
content=content.Replace("\r\n","\r");
int charCount = content.Length;
int lineCount=content.Split('\r').Length;
content = content.Replace('\r', ' ');
int WordsCount = content.Split(' ').Length;

}
}
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace CodingAlgorithms
{
    //Count the number of words in a string (Needs to handle multiple spaces between words)
    public static class WordCount
    {
        public static int Count(string x)
        {
            int result = 0;
 
            //Trim whitespace from beginning and end of string
            x = x.Trim();
 
            //Necessary because foreach will execute once with empty string returning 1
            if (x == "")
                return 0;
 
            //Ensure there is only one space between each word in the passed string
            while (x.Contains("  "))
                x = x.Replace("  ", " ");
 
            //Count the words
            foreach (string y in x.Split(' '))
                result++;
 
            return result;
        }
    }
}
 
Share this answer
 
Comments
CHill60 11-Dec-19 11:19am    
What if there are sentences in the file terminated with a period? Or a Question mark,or an Exclamation mark! What if there is a list separated by commas;or semi-colons?

Your code will return the incorrect number of words for the above comment (2 short of the actual number).

But really it's just a much longer version of Solution 3 - except you are not using the split option to remove the empty values that would result with multiple spaces, and then you actually loop through the returned values to count them instead of using the really handy property of a string array … Count. Those techniques were around 9 years ago when the question was asked, your solution looks like it was written for VB6!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900