Advertisement
the_alator

Untitled

Jun 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace LabX
  7. {
  8. class FileSentenseReader
  9. {
  10. static FileInfo defaultFile = new FileInfo(@"..\..\..\files\text.txt");
  11. static StreamReader reader;
  12. static String word;
  13.  
  14. public static void Start()
  15. {
  16. Console.Clear();
  17. Console.WriteLine("Enter the custom file name, or nothing to use default file");
  18.  
  19. FileInfo file;
  20.  
  21. String userFile = Program.GetCorrectString(0).Replace("\"", String.Empty);
  22. if (!File.Exists("userFile") || String.IsNullOrEmpty(userFile))
  23. file = defaultFile;
  24. else
  25. file = new FileInfo(userFile);
  26.  
  27. reader = new StreamReader(file.FullName, System.Text.Encoding.UTF8);
  28. Console.WriteLine("Is used " + file.Name + " file");
  29. Console.WriteLine("\nEnter the word to be count it the file");
  30. word = Program.GetCorrectString();
  31. Console.WriteLine();
  32.  
  33. char r;
  34. int checkIndex = 0;
  35. bool endOfSentense = false;
  36. int frequency = 0;
  37. while(reader.Peek() != -1)
  38. {
  39. r = (char) reader.Read();
  40. if(r != '\n' && r != '\r')
  41. Console.Write(r);
  42. if (word[checkIndex] == r){
  43. checkIndex++;
  44. if (checkIndex == word.Length)
  45. {
  46. frequency++;
  47. checkIndex = 0;
  48. }
  49. }else{
  50. checkIndex = 0;
  51. }
  52. if (r == '.')
  53. {
  54. endOfSentense = true;
  55. continue;
  56. }
  57. if (endOfSentense)
  58. {
  59. endOfSentense = false;
  60. if (r == ' ' || r == '\n' || r == '\r')
  61. {
  62. Console.WriteLine("\nFrequency: " + frequency + "\n");
  63. frequency = 0;
  64. }
  65. }
  66.  
  67.  
  68.  
  69. }
  70. //Console.Write(reader.ReadToEnd());
  71.  
  72.  
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement