Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. namespace Searcher
  7. {
  8.     public class Searcher
  9.     {
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             //String fileName = @"C:\Documents and Settings\9chat73\Desktop\count.txt";
  14.  
  15.             // To search dinamically, just ask for a file:
  16.             Console.WriteLine("Enter a file to search");
  17.  
  18.             String fileName = Console.ReadLine().Trim();
  19.  
  20.             if (File.Exists(fileName))
  21.             {
  22.                 var readOnlyFile = File.ReadAllText(fileName);
  23.                 Console.WriteLine("===============================");
  24.                 foreach (var words in readOnlyFile)
  25.                 {
  26.                     Console.Write(words);
  27.                 }
  28.                 Console.WriteLine("===============================");
  29.  
  30.                 Console.WriteLine("Type a word to search");
  31.                 String pattern = "\\b";
  32.                 pattern += Console.ReadLine();
  33.                 pattern += "\\b";
  34.                 // Do not forget to escape the pattern!
  35.                 int count = Regex.Matches(File.ReadAllText(fileName),
  36.                                          pattern,
  37.                                           RegexOptions.IgnoreCase).Count;
  38.                 // IgnoreCase = ignore case sens
  39.                 Console.WriteLine(count);
  40.             }
  41.             else
  42.             {
  43.                 Console.WriteLine("File Not Found");
  44.             }
  45.  
  46.             Console.ReadLine();
  47.         }
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement