Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("Please input the TXT file path in the next line");
  4.             var input = Console.ReadLine();
  5.             if (!string.IsNullOrEmpty(input) && File.Exists(input))
  6.             {
  7.                 // Find the letter frequency
  8.                 var frequency = Encoding.UTF8.GetString(Encoding.Default.GetBytes(File.ReadAllText(input))).ToUpper()
  9.                     .Where(c => Char.IsLetter(c))
  10.                     .GroupBy(c => c)
  11.                     .ToDictionary(g => g.Key, g => g.Count());
  12.                 // Print the total number of letters read
  13.                 Console.WriteLine("Total number of letters read: " + frequency.Sum(intValue => intValue.Value));
  14.                 // Order items in descending count value, only take the top 10 records
  15.                 foreach (var item in frequency.OrderByDescending(key => key.Value).Take(10))
  16.                     Console.WriteLine(item.Key + " (" + item.Value + ")");
  17.             }
  18.             else
  19.                 Console.WriteLine("File does not exist or cannot be read.");
  20.             Console.WriteLine("Please press the ENTER key to exit the application...");
  21.             Console.ReadLine();
  22.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement