Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- Console.WriteLine("Please input the TXT file path in the next line");
- var input = Console.ReadLine();
- if (!string.IsNullOrEmpty(input) && File.Exists(input))
- {
- // Find the letter frequency
- var frequency = Encoding.UTF8.GetString(Encoding.Default.GetBytes(File.ReadAllText(input))).ToUpper()
- .Where(c => Char.IsLetter(c))
- .GroupBy(c => c)
- .ToDictionary(g => g.Key, g => g.Count());
- // Print the total number of letters read
- Console.WriteLine("Total number of letters read: " + frequency.Sum(intValue => intValue.Value));
- // Order items in descending count value, only take the top 10 records
- foreach (var item in frequency.OrderByDescending(key => key.Value).Take(10))
- Console.WriteLine(item.Key + " (" + item.Value + ")");
- }
- else
- Console.WriteLine("File does not exist or cannot be read.");
- Console.WriteLine("Please press the ENTER key to exit the application...");
- Console.ReadLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement