コード例 #1
0
 public ActionResult CharacterCount(CharacterCountTool countTool)
 {
     if (ModelState.IsValid)
     {
         var charCounter = new CharacterCounter();
         var counts      = new CharacterCounts();
         counts = charCounter.CountCharacters(countTool.TextInput);
         countTool.Consonants      = counts.Consonants;
         countTool.ConsonantList   = counts.ConsonantList;
         countTool.Vowels          = counts.Vowels;
         countTool.VowelList       = counts.VowelList;
         countTool.Ys              = counts.Ys;
         countTool.Spaces          = counts.Spaces;
         countTool.TotalCharacters = counts.TotalCharacters;
         countTool.Punctuation     = counts.Punctuation;
         countTool.NonLetters      = counts.NonLetters;
         return(View("CharacterCount", countTool));
     }
     return(View("CharacterCount"));
 }
コード例 #2
0
        public CharacterCounts CountCharacters(String textInput)
        {
            var counts = new CharacterCounts();
            var text   = textInput.ToLower();

            foreach (var c in text)
            {
                if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
                {
                    counts.Vowels++;
                    counts.VowelList.Add(c);
                }

                else if (Char.IsWhiteSpace(c))
                {
                    counts.Spaces++;
                }

                else if (c == 'y')
                {
                    counts.Ys++;
                }

                else if (c == 'b' || c == 'd' || c == 'c' || c == 'f' || c == 'g' || c == 'h' || c == 'j' || c == 'k' ||
                         c == 'l' || c == 'm' || c == 'n' || c == 'p' || c == 'q' || c == 'r' || c == 's' || c == 't' ||
                         c == 'v' || c == 'w' || c == 'x' || c == 'z')
                {
                    counts.Consonants++;
                    counts.ConsonantList.Add(c);
                }

                else
                {
                    counts.Punctuation++;
                    counts.NonLetters.Add(c);
                }
            }
            counts.TotalCharacters = counts.Consonants + counts.Vowels + counts.Ys + counts.Punctuation + counts.Spaces;
            return(counts);
        }