Ejemplo n.º 1
0
 public void AddToList(LetterCount lc)
 {
     // if the alphabet string still contains the requested letter, we don't have an entry for it
     // so we can add it
     // replace the given letter in the alphabet string with a space
     // so next time we don't add the object again
     if (alphabet.Contains(lc.letter))
     {
         counts.Add(lc);
         alphabet = alphabet.Replace(lc.letter, ' ');
     }
 }
Ejemplo n.º 2
0
        public string total_letter_count(string s)
        {
            // deserialize the JSON payload to a key/value pair object
            KeyValuePair keyValuePair = JsonConvert.DeserializeObject <KeyValuePair>(s);

            // declare a new LetterCounts object to hold a list of all letters and their counts
            LetterCounts letterCounts = new LetterCounts();



            // set the key (??)
            letterCounts.key = keyValuePair.key;

            // iterate through each letter in the text
            foreach (char c in keyValuePair.value)
            {
                // see if the LetterCounts list already contains the letter

                // only get counts for alphabetic characters
                if (char.IsLetter(c))
                {
                    // get the letter count using a LINQ query
                    int count = (from n in keyValuePair.value
                                 where char.ToLower(n) == char.ToLower(c)    // match all cases
                                 select n).Count();


                    // each letter and its count will be a new object (LetterCount)
                    LetterCount letterCount = new LetterCount();

                    // set the letter and get its count from a LINQ query on the entire text value
                    letterCount.letter = char.ToLower(c);
                    letterCount.count  = count;

                    // add the LetterCount object to the LetterCounts list
                    letterCounts.AddToList(letterCount);
                }
            }

            // turn the LetterCounts object back into a JSON payload
            return(JsonConvert.SerializeObject(letterCounts, Formatting.Indented));
        }