C# program to count upper and lower case characters in a given string

CsharpServer Side ProgrammingProgramming

To count uppercase characters in a string, check the following condition −

myStr[i]>='A' &amp;&amp; myStr[i]<='Z'

To count lower case characters in a string, check the following condition −

myStr[i]>='a' &amp;&amp; myStr[i]<='z'

Example

You can try to run the following code to count upper and lower case characters in a given string.

Live Demo

using System;
public class Demo {
   public static void Main() {
      string myStr;
      int i, len, lower_case, upper_case;
      myStr = "Hello";
      Console.Write("String: "+myStr);
      lower_case = 0;
      upper_case = 0;
      len = myStr.Length;
      for(i=0; i<len; i++) {
         if(myStr[i]>='a' &amp;&amp; myStr[i]<='z') {
            lower_case++;
         } else if(myStr[i]>='A' &amp;&amp; myStr[i]<='Z') {
            upper_case++;
         }
      }
      Console.Write("\nCharacters in lowecase: {0}\n", lower_case);
      Console.Write("Characters in uppercase: {0}\n\n", upper_case);
   }
}

Output

String: Hello
Characters in lowecase: 4
Characters in uppercase: 1
raja
Published on 25-Jul-2018 10:59:39
Advertisements