2

The Title says everything..

Searched Google and Stackoverflow and didn't find something similiar..

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
eMi
  • 5,540
  • 10
  • 60
  • 109

3 Answers3

3

For .txt file you can use regexp \b\w+\b. It will match all occurrences of words, e.g.:

var count = Regex.Matches(input, @"\b\w+\b").Count;

To count letters:

int count = input.Count(char.IsLetter);
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • Thank you, that worked fine - to count the words.. Now just need the letters :D – eMi Aug 19 '11 at 09:01
  • my SolutioN: private int GetLetterCount() { int count = 0; StreamReader tr = new StreamReader("hello.txt"); string text = tr.ReadToEnd(); foreach (char c in text) { if (!char.IsWhiteSpace(c)) { count++; } } return count; } – eMi Aug 19 '11 at 09:10
  • Your method will count digits, punctuation, letters, i.e. all chars excluding whitespaces. – Kirill Polishchuk Aug 19 '11 at 09:16
1
static void Main()
{
     const string t1 = "To be or not to be, that is the question.";
     Console.WriteLine(WordCounting.CountWords1(t1));
     Console.WriteLine(WordCounting.CountWords2(t1));

     const string t2 = "Mary had a little lamb.";
     Console.WriteLine(WordCounting.CountWords1(t2));
     Console.WriteLine(WordCounting.CountWords2(t2));
}

more is here

genesis
  • 50,477
  • 20
  • 96
  • 125
0

getting text from rtf - Get plain text from an RTF text

count words in text - http://www.dotnetperls.com/word-count

Community
  • 1
  • 1
Mike
  • 18,257
  • 1
  • 18
  • 12