Write a program to Count Number of Uppercase and Lowercase letters


This program counts the number of upper and lower case characters in a given string. Here's how it works:

  • It takes a string as input and converts it to a StringBuffer object.
  • It initializes the count of upper and lower case characters to 0.
  • It iterates over each character in the string using a for loop.
  • For each character, it checks whether it is an upper or lower case character using the Character.isUpperCase() and Character.isLowerCase() methods.
  • If the character is upper case, it increments the count of upper case characters by 1. If the character is lower case, it increments the count of lower case characters by 1.
  • Finally, it prints the counts of upper and lower case characters.

This program can be useful in various applications where you need to analyze the character distribution in a given string.

Source Code

class Count_Case
{
	static void charCase_Count(StringBuffer str)
	{
		int lwr=0,upr=0,len = str.length();
		for (int i = 0; i < len; i++) {
			Character c = str.charAt(i);
			if (Character.isLowerCase(c))
				lwr+=1;
			else if (Character.isUpperCase(c))
				upr+=1;
			// else
				// oth+=1;
		}
 
		System.out.println("Number of UpperCase  : "+upr);
		System.out.println("Number of LowerCase  : "+lwr);
	}
	public static void main(String[] args)
	{
		StringBuffer str = new StringBuffer("Tutor Joes");
		charCase_Count(str);
	}
}

Output

Number of UpperCase  : 2
Number of LowerCase  : 7

Example Programs