Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. namespace Searchers
  6. {
  7.     public class Searcher
  8.     {
  9.         public static bool IsExist(string fileName)
  10.         {
  11.             string errorMessage = "The file: " + fileName + " could not be found! Please enter a valid file name!";
  12.  
  13.             if (!File.Exists(fileName))
  14.             {
  15.                 Console.WriteLine(errorMessage);
  16.                 return false;
  17.             }
  18.             else
  19.             {
  20.                 return true;
  21.             }
  22.  
  23.         }
  24.         public static int GetWordsCount(string file)
  25.         {
  26.             var result = file.Split(' ').ToList();
  27.             return result.Count;
  28.  
  29.         }
  30.         public static int GetSymbolsCount(string file)
  31.         {
  32.             var counter = 0;
  33.             for (int i = 0; i < file.Length; i++)
  34.             {
  35.                 counter++;
  36.             }
  37.             return counter;
  38.         }
  39.         public static void WriteOutput(string fileName)
  40.         {
  41.             var readOnlyFile = File.ReadAllText(fileName);
  42.             Console.WriteLine("===============================");
  43.             Console.WriteLine(readOnlyFile);
  44.             Console.WriteLine("===============================");
  45.             Console.WriteLine("The file contains " + GetSymbolsCount(readOnlyFile) + " symbols");
  46.             Console.WriteLine("The file contains ~ " + GetWordsCount(readOnlyFile) + " words");
  47.  
  48.             Console.WriteLine("Type a word to search");
  49.  
  50.             String searchInput = Console.ReadLine();
  51.             ImplementLogic(searchInput, fileName);
  52.  
  53.             }
  54.  
  55.         public static void ImplementLogic(string searchInput, string filename)
  56.         {
  57.             while (searchInput != "!!exit")
  58.             {
  59.                 String pattern = "\\b";
  60.                 pattern += searchInput;
  61.                 pattern += "\\b";
  62.                 int count = Regex.Matches(File.ReadAllText(filename),
  63.                     pattern,
  64.                     RegexOptions.IgnoreCase).Count;
  65.                 // IgnoreCase = ignore case sens
  66.  
  67.                 if (count == 1)
  68.                 {
  69.                     Console.WriteLine($"The word \"{searchInput}\" has been found {count} time.");
  70.                 }
  71.                 else
  72.                 {
  73.                     Console.WriteLine($"The word \"{searchInput}\" has been found {count} times.");
  74.                 }
  75.                 Console.WriteLine("Search a new word, or type !!exit");
  76.  
  77.                 searchInput = Console.ReadLine();
  78.             }
  79.  
  80.         }
  81.  
  82.         public static void Result()
  83.         {
  84.             // To search dinamically, just ask for a file:
  85.             Console.WriteLine("Enter a file to search. Example: words.txt");
  86.  
  87.             String fileName = Console.ReadLine().Trim();
  88.  
  89.             if (IsExist(fileName))
  90.             {
  91.                 WriteOutput(fileName);
  92.             }
  93.             else
  94.             {
  95.                 while (!IsExist(fileName))
  96.                 {
  97.                     fileName = Console.ReadLine();
  98.                
  99.                 }
  100.                 if (IsExist(fileName))
  101.                 {
  102.                     WriteOutput(fileName);
  103.                 }
  104.             }
  105.         }
  106.  
  107.         public static void Main()
  108.         {
  109.             Result();
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement