Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- /*
- * Write a program that reads a string from the console and lists all
- * different words in the string along with information how many times each word is found.
- */
- namespace CounterDifferentWords
- {
- class CounterDifferentWords
- {
- private static void PrintDifferentWordsAndCount(Dictionary<string, int> dictionary)
- {
- foreach (string str in dictionary.Keys)
- {
- Console.WriteLine("{0} -> {1} times.", str, dictionary[str]);
- }
- }
- private static Dictionary<string, int> CountDifferentWordsInText(string[] words)
- {
- Dictionary<string, int> dictionary = new Dictionary<string, int>();
- foreach (string str in words)
- {
- if (!dictionary.ContainsKey(str))
- {
- dictionary.Add(str, 1);
- }
- else
- {
- dictionary[str]++;
- }
- }
- return dictionary;
- }
- static void Main(string[] args)
- {
- string input = "Write a program that reads that string from the console the lists all";
- Dictionary<string, int> dictionary = new Dictionary<string, int>();
- input = input.ToLower();
- string[] words = input.Split(' ', '.', ',');
- dictionary = CountDifferentWordsInText(words);
- PrintDifferentWordsAndCount(dictionary);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement