Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. static void Main(string[] args)
  2.         {
  3.             string s = "kutya,cica,mérési,hiba,kutya,kutya,kutya,lofasz,autó,cékla,cica,hiba,hiba,hiba,alma";
  4.             WordCounter(s);
  5.  
  6.             Console.ReadKey();
  7.            
  8.         }
  9.  
  10.         /// <summary>
  11.         /// A vesszővel elválasztott szavak előfordulásást számolja meg és írja ki
  12.         /// </summary>
  13.         /// <param name="words"></param>
  14.         private static void WordCounter( string words)
  15.         {
  16.             if(string.IsNullOrWhiteSpace(words) || !words.Contains(","))
  17.             {
  18.                 throw new ArgumentException("input string must contains , ", "words");
  19.             }
  20.             string[] separatedWords = words.Split(',');
  21.  
  22.             Dictionary<string, int> wordsDict = new Dictionary<string, int>();
  23.  
  24.             foreach( var item in separatedWords)
  25.             {
  26.                 if(!wordsDict.ContainsKey(item))
  27.                 {
  28.                     wordsDict.Add(item, 1);
  29.                 }
  30.                 else
  31.                 {
  32.                     wordsDict[item] += 1;
  33.                 }
  34.             }
  35.  
  36.             for (int i = 0; i < wordsDict.Count; i++)
  37.             {
  38.                 Console.WriteLine(wordsDict.ElementAt(i).Key + " " + wordsDict.ElementAt(i).Value);
  39.             }
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement