Count number of words in a text file
Hello guys,please I need a program that will count the number of words in a text file.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 4, 2015, 12:46 AM EST
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Mar 3, 2015, 10:40 PM EST
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;
}