Count Vowels,Capital letters,small letters,numbers and space in Java


This Java program counts the number of uppercase letters, lowercase letters, spaces, numbers, vowels, and symbols in a given string using a StringBuilder. Here is how the program works:

  • A StringBuilder object a is created with the string "Ram-age is 12@".
  • Counters for uppercase letters, lowercase letters, spaces, numbers, and vowels are initialized to 0.
  • A for loop is used to iterate over each character in the StringBuilder object.
  • If the character is an uppercase letter (ASCII value between 65 and 90), the upper counter is incremented.
  • If the character is a lowercase letter (ASCII value between 97 and 122), the lower counter is incremented.
  • If the character is a space (ASCII value 32), the space counter is incremented.
  • If the character is a number (ASCII value between 48 and 57), the number counter is incremented.
  • If the character is a vowel (either uppercase or lowercase), the vowels counter is incremented.
  • The total number of symbols in the string is calculated by subtracting the sum of upper, lower, space, and number from the length of the string.
  • The final counts are printed out to the console.

Note: This program assumes that the input string contains only ASCII characters. If the string contains non-ASCII characters, the behavior of this program is undefined.

Source Code

public class countCharacter {
    public static void main(String args[])
    {
        //Program to Count Uppercase,Lowercase,Space,Number,Vowels and Symbols in a String
        StringBuilder a = new StringBuilder("Ram-age is 12@");
        System.out.println(a);
        int upper = 0, lower = 0, space = 0, number = 0, vowels = 0;
        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) >= 97 && a.charAt(i) <= 122) {
                lower++;
            }
            if (a.charAt(i) >= 65 && a.charAt(i) <= 90) {
                upper++;
            }
            if (a.charAt(i) == 32) {
                space++;
            }
            if (a.charAt(i) >= 48 && a.charAt(i) <= 57) {
                number++;
            }
            if (a.charAt(i) == 'A' || a.charAt(i) == 'E' || a.charAt(i) == 'I' || a.charAt(i) == 'O' ||
                    a.charAt(i) == 'U' || a.charAt(i) == 'a' || a.charAt(i) == 'e' || a.charAt(i) == 'i' ||
                    a.charAt(i) == 'o' || a.charAt(i) == 'u') {
                vowels++;
            }
        }
        System.out.println("Uppercase   : "+upper);
        System.out.println("Lowercase   : "+lower);
        System.out.println("Space       : "+space);
        System.out.println("Number      : "+number);
        System.out.println("Vowels      : "+vowels);
        System.out.println("Symbols      : "+(a.length()-(upper+lower+space+number)));
    }
}
 

Output

Ram-age is 12@
Uppercase   : 1
Lowercase   : 7
Space       : 2
Number      : 2
Vowels      : 4
Symbols      : 2
To download raw file Click Here

Basic Programs