Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions WorkingWithFiles/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ public static class WorkingWithFiles
// .Trim() might be useful in this situation.
public static int WordCount(string fileName)
{
return default;
List<string> words = new List<string>();

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.
Expand All @@ -26,15 +41,36 @@ public static int WordCount(string fileName)
// .Trim() might be useful in this situation.
public static string LongestWord(string fileName)
{
return default;
List<string> words = new List<string>();

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;
}
}

public static class Program
{
public static void Main()
{

}
}
}