From c6d15936f010a948e9f4c2a1b996f57877b6309b Mon Sep 17 00:00:00 2001 From: coopian <87322199+coopian@users.noreply.github.com> Date: Mon, 19 Jul 2021 13:40:38 -0500 Subject: [PATCH] Completed --- WorkingWithFiles/Program.cs | 42 ++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/WorkingWithFiles/Program.cs b/WorkingWithFiles/Program.cs index 15634e0..2aebad0 100644 --- a/WorkingWithFiles/Program.cs +++ b/WorkingWithFiles/Program.cs @@ -17,7 +17,22 @@ public static class WorkingWithFiles // .Trim() might be useful in this situation. public static int WordCount(string fileName) { - return default; + List words = new List(); + + string path = @"C:\Users\SSgt Cooper\Documents\GitHub\WorkingWithFiles\WorkingWithFilesTest"; + string line; + int count = 0; + + using (StreamReader file = new StreamReader(new FileStream(Path.Combine(path, fileName) , FileMode.Open))) + { + while ((line = file.ReadLine()) != null) + { + words.AddRange(line.Trim().Split(' ')); + } + count += words.Count; + } + + return count; } // 2- Write a method that reads a text file and returns the longest word in the file. Ex. @@ -26,7 +41,28 @@ public static int WordCount(string fileName) // .Trim() might be useful in this situation. public static string LongestWord(string fileName) { - return default; + List words = new List(); + + string path = @"C:\Users\SSgt Cooper\Documents\GitHub\WorkingWithFiles\WorkingWithFilesTest"; + string line; + string longest; + + using (StreamReader file = new StreamReader(new FileStream(Path.Combine(path, fileName), FileMode.Open))) + { + if (file.Peek() < 0) + { + longest = "File is Empty"; + } + else + { + while ((line = file.ReadLine()) != null) + { + words.AddRange(line.Trim().Split(' ')); + } + longest = words.OrderByDescending(n => n.Length).First(); + } + } + return longest; } } @@ -34,7 +70,7 @@ public static class Program { public static void Main() { - + } } }