Description
To count total number of upper case, check how many character are from A-Z and to count total number of lower case, check how many character are from a-z in string.
C/C++
/* C Program to count uppercase and lowercase alphabet characters in String */ //Save it as CountUppercaseLowercase.c #include<stdio.h> int main(){ char str[100]; printf("Enter a string : "); gets(str); int i, countUpper=0, countLower=0; for(i=0;i<strlen(str);i++) { if(str[i] >= 'A' && str[i] <= 'Z') { countUpper++; } if(str[i] >= 'a' && str[i] <= 'z') { countLower++; } } printf("Uppercase : %d", countUpper); printf("\nLowercase : %d", countLower); return 0; }
Input: Enter a string : Hello123JoHn Output: Uppercase : 3 Lowercase : 6
Java
/* Java Program to count uppercase and lowercase alphabet characters in String*/ //Save it as CountUppercaseLowercase.java import java.io.*; import java.util.Scanner; public class CountUppercaseLowercase { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a string : "); String str = scanner.nextLine(); int countUpper=0, countLower=0; for(int i=0;i<str.length();i++) { if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') { countUpper++; } if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') { countLower++; } } System.out.println("Uppercase : "+countUpper); System.out.println("Lowercase : "+countLower); } }
Input: Enter a string : Hello123JoHn Output: Uppercase : 3 Lowercase : 6
Related Programs
1) Program to sort characters of strings in alphabetical order2) Program to Sort set of strings in alphabetical order
3) Program to Convert Lowercase to Uppercase of a String
4) Program to Append one String to another String
5) Program to Compare Two Strings
6) Program to Reverse a String
7) Find Substring of a Given String
8) Program to Insert a String into Another String
9) Program to check String is Palindrome
10) Program to check entered character is vowel or consonants
11) Program to remove vowels from a String