From 319bff1c12060f567e2dae97012c81d981295e1d Mon Sep 17 00:00:00 2001 From: MikeHickey17 <79542137+MikeHickey17@users.noreply.github.com> Date: Mon, 27 Sep 2021 12:56:17 -0500 Subject: [PATCH] Update Program.cs --- WorkingWithFiles/Program.cs | 50 +++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/WorkingWithFiles/Program.cs b/WorkingWithFiles/Program.cs index 15634e0..6721300 100644 --- a/WorkingWithFiles/Program.cs +++ b/WorkingWithFiles/Program.cs @@ -17,7 +17,27 @@ public static class WorkingWithFiles // .Trim() might be useful in this situation. public static int WordCount(string fileName) { - return default; + int result = 0; + string line = ""; + string[] words; + if (File.Exists(fileName)) + { + string[] content = File.ReadAllLines(fileName); + if (content.Length == 0) + { + return result; + } + else + { + for (int i = 0; i < content.Length; i++) + { + line = content[i].Trim(); + words = line.Split(" .,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + result += words.Count(); + } + } + } + return result; } // 2- Write a method that reads a text file and returns the longest word in the file. Ex. @@ -26,7 +46,33 @@ public static int WordCount(string fileName) // .Trim() might be useful in this situation. public static string LongestWord(string fileName) { - return default; + string result = ""; + string line = ""; + string[] words; + if (File.Exists(fileName)) + { + string[] content = File.ReadAllLines(fileName); + if (content.Length == 0) + { + return "File is Empty"; + } + else + { + for (int i = 0; i < content.Length; i++) + { + line = content[i].Trim(); + words = line.Split(" .,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + for (int j = 0; j < words.Length; j++) + { + if (words[j].Length > result.Length) + { + result = words[j]; + } + } + } + } + } + return result; } }