using System; using System.Text; using System.Text.RegularExpressions; namespace StupidWordCounter { class StupidWordCounterModel { // variables for communication between model and controller public static String strSentence; public static String strNumberOfLetters; public static String strNumberOfWords; public static void CountLettersAndWords() { // remove leading and trailing whitespaces strSentence = strSentence.Trim(); // make all whitespaces appear only once strSentence = Regex.Replace(strSentence, " +", " "); // count spaces (we assume that anything delimited by spaces is a word) // I realise that is completely not true, so feel free to modify this file. int spaces = 0; int i = 0; while (i < strSentence.Length) { if (strSentence.Substring(i, 1).Equals(@" ")) { spaces = spaces + 1; } i = i + 1; } // deduce number of words from number of spaces int words = spaces + 1; // remove spaces (we count anything else as "letters") strSentence = strSentence.Replace(" ", ""); // count rest int letters = strSentence.Length; // write results into static strings strNumberOfLetters = letters.ToString(); strNumberOfWords = words.ToString(); } } }