简体   繁体   English

C#计算字符串中大写和小写字母的数量

[英]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) 大写和小写字母的值是0。(不应该)

Any suggestions? 有什么建议么? Thanks! 谢谢!

You don't need else before 你不需要else

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. 您有一系列if / else语句,并且匹配的第一个条件(元音或辅音)将阻止将来的任何条件匹配。 Break the if / else chain into 2 chains: if / else链分成2个链:

  • 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 我已经使用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 根据Sajeetharan的回答稍快一些的版本

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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