Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C# program to count upper and lower case characters in a given string
To count uppercase characters in a string, check the following condition −
myStr[i]>='A' && myStr[i]<='Z'
To count lower case characters in a string, check the following condition −
myStr[i]>='a' && myStr[i]<='z'
Example
You can try to run the following code to count upper and lower case characters in a given string.
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' && myStr[i]<='z') {
lower_case++;
} else if(myStr[i]>='A' && myStr[i]<='Z') {
upper_case++;
}
}
Console.Write("\nCharacters in lowecase: {0}
", lower_case);
Console.Write("Characters in uppercase: {0}
", upper_case);
}
}
Output
String: Hello Characters in lowecase: 4 Characters in uppercase: 1
Advertisements
