Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace LabX
- {
- class FileSentenseReader
- {
- static FileInfo defaultFile = new FileInfo(@"..\..\..\files\text.txt");
- static StreamReader reader;
- static String word;
- public static void Start()
- {
- Console.Clear();
- Console.WriteLine("Enter the custom file name, or nothing to use default file");
- FileInfo file;
- String userFile = Program.GetCorrectString(0).Replace("\"", String.Empty);
- if (!File.Exists("userFile") || String.IsNullOrEmpty(userFile))
- file = defaultFile;
- else
- file = new FileInfo(userFile);
- reader = new StreamReader(file.FullName, System.Text.Encoding.UTF8);
- Console.WriteLine("Is used " + file.Name + " file");
- Console.WriteLine("\nEnter the word to be count it the file");
- word = Program.GetCorrectString();
- Console.WriteLine();
- char r;
- int checkIndex = 0;
- bool endOfSentense = false;
- int frequency = 0;
- while(reader.Peek() != -1)
- {
- r = (char) reader.Read();
- if(r != '\n' && r != '\r')
- Console.Write(r);
- if (word[checkIndex] == r){
- checkIndex++;
- if (checkIndex == word.Length)
- {
- frequency++;
- checkIndex = 0;
- }
- }else{
- checkIndex = 0;
- }
- if (r == '.')
- {
- endOfSentense = true;
- continue;
- }
- if (endOfSentense)
- {
- endOfSentense = false;
- if (r == ' ' || r == '\n' || r == '\r')
- {
- Console.WriteLine("\nFrequency: " + frequency + "\n");
- frequency = 0;
- }
- }
- }
- //Console.Write(reader.ReadToEnd());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement