I am working on a small code that searches an input text file (of my choice). I am creating a search function. So far I got it to display how many times the search word occurs in the text file and also the line number. I need some help on displaying how the searched word appears in the text file. For example, If I search for the word "the" and it appears as "THE" or "The" in the text file, I would want to display that on my console window.
Any help, advice, or suggestion is appreciated. Thank you in advance!
Here is my code:
string line;
int counter = 0;
Console.WriteLine("Enter a word to search for: ");
string userText = Console.ReadLine();
string file = "NewTextFile.txt";
StreamReader myFile = new StreamReader(file);
int found = 0;
while ((line = myFile.ReadLine()) != null)
{
counter++;
if (line.Contains(userText))
{
Console.WriteLine("Found on line number: {0}", counter);
found++;
}
}
Console.WriteLine("A total of {0} occurences found", found);