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
50 changes: 48 additions & 2 deletions WorkingWithFiles/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}

Expand Down