svetlozar_kirkov

Split words in text and count them (Exercise)

Oct 11th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleTests
  4. {
  5.     class ConsoleTests
  6.     {
  7.         static void Main()
  8.         {
  9.             string text = Console.ReadLine();
  10.             char[] separators = new char[] {'.', ' ', ','};
  11.             string[] words = text.ToLower().Split(separators,StringSplitOptions.RemoveEmptyEntries);
  12.             Array.Sort(words);
  13.             int count = 1;
  14.             for (int i = 0; i < words.Length && i + 1 < words.Length; i++)
  15.             {
  16.                 if (words[i] == words[i + 1])
  17.                 {
  18.                     count++;
  19.                 }
  20.                 else
  21.                 {
  22.                     Console.WriteLine("{0} --> {1}", words[i], count);
  23.                     count = 1;
  24.                 }
  25.             }
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment