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:
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.
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))); } }
Ram-age is 12@ Uppercase : 1 Lowercase : 7 Space : 2 Number : 2 Vowels : 4 Symbols : 2To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions