Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- string s = "kutya,cica,mérési,hiba,kutya,kutya,kutya,lofasz,autó,cékla,cica,hiba,hiba,hiba,alma";
- WordCounter(s);
- Console.ReadKey();
- }
- /// <summary>
- /// A vesszővel elválasztott szavak előfordulásást számolja meg és írja ki
- /// </summary>
- /// <param name="words"></param>
- private static void WordCounter( string words)
- {
- if(string.IsNullOrWhiteSpace(words) || !words.Contains(","))
- {
- throw new ArgumentException("input string must contains , ", "words");
- }
- string[] separatedWords = words.Split(',');
- Dictionary<string, int> wordsDict = new Dictionary<string, int>();
- foreach( var item in separatedWords)
- {
- if(!wordsDict.ContainsKey(item))
- {
- wordsDict.Add(item, 1);
- }
- else
- {
- wordsDict[item] += 1;
- }
- }
- for (int i = 0; i < wordsDict.Count; i++)
- {
- Console.WriteLine(wordsDict.ElementAt(i).Key + " " + wordsDict.ElementAt(i).Value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement