简体   繁体   中英

C# Counting the number of upper and lower case letters in a string

Hi I've been asked to do a task which calculates the number of vowels, consonants, upper and lower case letters in a string.

I'm having trouble working out upper and lower case letters in a string. I can successfully count the number of vowels and constants but upper and lower case letters seems to be a pain.

Here's the code:

    public void Calculate()
    {
        foreach(string sentence in sentenceList)
        {
            sentences++;

            for (int i = 0; i < sentence.Length; i++)
            {
                if (vowelsArray.Contains(sentence[i]))
                {
                    vowels++;
                }
                else if (consonantsArray.Contains(sentence[i]))
                {
                    consonants++;
                }
                else if (char.IsUpper(sentence[i]))
                {
                    upperCaseLetters++;
                }
                else if (char.IsLower(sentence[i]))
                {
                    lowerCaseLetters++;
                }
            }
        }
    }

The value for the upper and lower case letters is 0. (It shouldn't be)

Any suggestions? Thanks!

You don't need else before

else if (char.IsUpper(sentence[i]))

Because you have two independent sets of conditions:

  1. Vowel / Consonant
  2. UpperCase / LowerCase

You have a chain of if / else statements and the first condition that is matched (either the vowels or consonants) will prevent any future conditions from being matched. Break the if / else chain into 2 chains:

  • Vowels vs Consonants
  • Uppercase vs Lowercase

See updated code below:

    public void Calculate()
    {
        foreach(string sentence in sentenceList)
        {
            sentences++;

            for (int i = 0; i < sentence.Length; i++)
            {
                if (vowelsArray.Contains(sentence[i]))
                {
                    vowels++;
                }
                else if (consonantsArray.Contains(sentence[i]))
                {
                    consonants++;
                }

                // the else was removed here!
                if (char.IsUpper(sentence[i]))
                {
                    upperCaseLetters++;
                }
                else if (char.IsLower(sentence[i]))
                {
                    lowerCaseLetters++;
                }
            }
        }
    }

您每次在循环中只会遇到一个条件,因此,如果您的前两个条件涵盖了所有可能性(这很可能,因为它涵盖了所有元音和所有辅音!),您永远不会达到第三个条件。第四块。

  string text = "This is Sample";
  int upcount = 0;
  int lowcount = 0;
  for (int i = 0; i < text.Length; i++)
  {
   if (char.IsUpper(text[i])) upcount++;
   if (char.IsLower(text[i])) lowcount++;
  }
  Console.Write(upcount);
  Console.Write(lowcount);

EDIT

In your case change it like this,

if (char.IsUpper(sentence[i]))
{
    upperCaseLetters++;
}
else (char.IsLower(sentence[i]))
{
    lowerCaseLetters++;
}

I have tested this using dotnetfiddle

But here is the code that worked for me:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        int upper = 0;
        int lower = 0;

        string upperLowerCase = "This Is A Test";

        char[] splitString = upperLowerCase.ToCharArray();

        for(int i = 0; i < splitString.Length; i++)
        {
            if(char.IsUpper(splitString[i]))
            {
                upper++;    
            }
            else
            {
                lower++;    
            }

        }

        Console.WriteLine("Total Upper Case Letters: " + upper.ToString());
        Console.WriteLine("Total Lower Case Letters: " +lower.ToString());
    }
}

// Output
// Total Upper Case Letters: 4
// Total Lower Case Letters: 10

But in your case you need to separate conditional statements. One to check for vowels or consonants and the other to check the case of the letter.

So this:

else if (char.IsUpper(sentence[i]))
{
    upperCaseLetters++;
}
else if (char.IsLower(sentence[i]))
{
    lowerCaseLetters++;
}

Needs to be changed to:

if (char.IsUpper(sentence[i]))
{
    upperCaseLetters++;
}
else
{
    lowerCaseLetters++;
}

I hope this helps!

Slightly faster version based on Sajeetharan's answer

 string text = "This is Sample";
  int upcount = 0;
  int lowcount = 0;
  for (int i = 0; i < text.Length; i++)
  {
  if (text[i]>64 && text[i]<91) upcount++;
   else if (text[i]>96 && text[i]<123) lowcount++;
  }
  Console.Write(upcount);
  Console.Write(lowcount);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM