Files and Searching in C#

Files and Searching in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Searchinfile
{
class Program
{
static void Main(string[] args)
{

string[] lines=File.ReadAllLines(@"C:\Users\Haroon\Desktop\test.txt");
string keyword;
Console.WriteLine("Enter a word to search : ");
keyword = Console.ReadLine();
int linenum = 0;
foreach (string s in lines)
{
linenum = linenum + 1;
char[] tokens={' ','.',',','?','-'};
string[] words = s.Split(tokens);
int wordnum = 0;
foreach (string w in words)
{
Console.WriteLine(w);
wordnum = wordnum + 1;
if (w.ToUpper() == keyword.ToUpper())
{ Console.WriteLine("Present in Line number: " + linenum+" Word # : 
"+wordnum); }
}
}
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *