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# 2.94 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.    
  8.     public class Searcher
  9.     {
  10.  
  11.         public static bool isExist(string fileName)
  12.         {
  13.             string errorMessage= "The file: " + fileName + " could not be found!";
  14.            
  15.             if (!File.Exists(fileName))
  16.             {
  17.                 Console.WriteLine(errorMessage);
  18.                 return false;
  19.             }
  20.             else
  21.             {
  22.                 return true;
  23.             }
  24.  
  25.          
  26.         }
  27.         public static int GetWordsCount(string file)
  28.         {
  29.             var result = file.Split(' ').ToList();
  30.             return result.Count;
  31.  
  32.         }
  33.         public static int GetSymbolsCount(string file)
  34.         {
  35.             var counter = 0;
  36.             foreach (var symbols in file)
  37.             {
  38.                 counter++;
  39.             }
  40.             return counter;
  41.         }
  42.         public static void Result(string fileName)
  43.         {
  44.            
  45.             var readOnlyFile = File.ReadAllText(fileName);
  46.             Console.WriteLine("===============================");
  47.             Console.WriteLine(readOnlyFile);
  48.             Console.WriteLine("===============================");
  49.             Console.WriteLine("The file contains " + GetSymbolsCount(readOnlyFile) + " symbols");
  50.             Console.WriteLine("The file contains ~ " + GetWordsCount(readOnlyFile) + " words");
  51.  
  52.             Console.WriteLine("Type a word to search");
  53.            
  54.          
  55.            
  56.             String searchInput = Console.ReadLine();
  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.                 Console.WriteLine(" --- {0}  ===> {1}",searchInput,count);
  67.                 Console.WriteLine("Search a new word, or type !!exit");
  68.              
  69.                 searchInput = Console.ReadLine();
  70.  
  71.             }
  72.            
  73.             // Do not forget to escape the pattern!
  74.          
  75.         }
  76.         public static void Main()
  77.         {
  78.             // To search dinamically, just ask for a file:
  79.             Console.WriteLine("Enter a file to search. Example: words.txt");
  80.  
  81.             String fileName = Console.ReadLine().Trim();
  82.  
  83.             if (isExist(fileName))
  84.             {
  85.                 Result(fileName);
  86.             }
  87.             else
  88.             {
  89.                 Console.WriteLine("Please enter a valid file name!");
  90.                 fileName = Console.ReadLine();
  91.                 if (isExist(fileName))
  92.                 {
  93.                     Result(fileName);
  94.                 }
  95.             }
  96.            
  97.          
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement