Click here to Skip to main content
12,956,234 members (66,532 online)
Rate this:
 
Please Sign up or sign in to vote.
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 15-Jun-10 10:43am
jaramh345
Updated 15-Jun-10 11:22am
Moak11.6K
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?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It's easy to do. Really. Just be a programmer and write it.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

string[] source = descripicion.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);

int total_words = source.Count();
  Permalink  
Comments
KARTHIK Bangalore 2-Jan-14 3:47am
   
5, good answer, simple too.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Here - try this.

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;
}
  Permalink  
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.
   
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. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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;

}
}
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,243
ppolymorphe 2,070
Jochen Arndt 1,973


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 2 Jan 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100