using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void CountWords(string input) { //A place to store all the words and the count var result = new Dictionary(); foreach (string word in input.Split(' ')) { //variable I'm not going to use... int count; //Check if word is already exists. I added .ToLower() to make it case insensitive. if (result.TryGetValue(word, out count)) { //It already exists, so I increase the count by 1 result[word]++; } else { //It is new, so I add the word, with a count of 1. result.Add(word, 1); } } //Order results var OrderedResult = result.OrderBy(l => l.Key); //Print results foreach (KeyValuePair item in OrderedResult) { Console.WriteLine("Word: {0}, count: {1}", item.Key, item.Value); } } private void button1_Click(object sender, EventArgs e) { CountWords("My face is not much like your face"); } } }