public static void wordscount(string word, string path)
{
int index;
int occurrence = 0;
try
{
StreamReader reader1 = new StreamReader(path);
using(reader1)
{
string line = reader1.ReadLine();
while (line != null)
{
index = line.IndexOf(word);
while (index != -1)
{
occurrence++;
index = line.IndexOf(word,(index + 1));
}
line = reader1.ReadLine();
}
Console.WriteLine("the word {0} occours in {1} times", word, occurrence);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}A method to count the number of occurrence of a word in a text file.
