Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C C# GimmeCode
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
jaramh339
Edited 15-Jun-10 11:22am
Moak11.4K
v3
Comments
Sandeep Mewara at 15-Jun-10 17:05pm
   
'I want...'... ok. We got that. Now, tell us what have you tried? Show us your effort...
daveauld at 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 at 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 at 15-Jun-10 18:48pm
   
thanks
Hans Dietrich at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 2 Jan 2014
Copyright © CodeProject, 1999-2014
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