JAVA program to count the number of vowels, consonants, digits, white spaces & special characters in a given string
This JAVA program is to count the number of vowels, consonants, digits, white spaces & special characters in a given string.
For example, string “code 21” will have 2 consonants(c,d),2 vowels(o,e), 2 digits(2,1), 1 whitespace and 1 special character(;).
Logic
We just use a for loop and for each parameter we keep an if condition along with a counter for each parameter.
Dry Run of the Program
Take input string s. Let us take s=code 21;
Convert String str to charArray str[].
Find length of str[] using length method and store it in variable n;
1st iteration for(i=0;i<n;i++) i.e. for(i=0;0<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) false
else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’)) true str[0]=’c’
consonants++ hence consonants=1
2nd iteration for(i=1;i<n;i++) i.e. for(i=1;1<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) true as str[1]=’o’
vowels++; hence vowels=1
3rd iteration for(i=2;i<n;i++) i.e. for(i=2;2<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) false
else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’)) true as str[2]=’d’
consonants++ hence consonants=2
4th iteration for(i=3;i<n;i++) i.e. for(i=3;3<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) true as str[3]=’e’
vowels++ hence vowels=2
else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’)) false
5th iteration for(i=4;i<n;i++) i.e. for(i=4;4<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) false
else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’)) false
else if(str[i]>=’0′ && str[i]<=’9′) false
else if (str[i]==’ ‘) true as str[4]=’ ‘
spaces++ hence spaces=1
6th iteration for(i=5;i<n;i++) i.e. for(i=5;5<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) false
else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’)) false
else if(str[i]>=’0′ && str[i]<=’9′) true as str[5]=2
digits++ hence digits=1
7th iteration for(i=6;i<n;i++) i.e. for(i=6;6<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) false
else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’)) false
else if(str[i]>=’0′ && str[i]<=’9′) true as str[6]=1
digits++ hence digits=2
8th iteration for(i=7;i<n;i++) i.e. for(i=7;7<8;i++)
if(str[i]==’a’ || str[i]==’e’ || str[i]==’i’ ||str[i]==’o’ || str[i]==’u’ || str[i]==’A’ ||str[i]==’E’ || str[i]==’I’ || str[i]==’O’ ||str[i]==’U’) false
else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’)) false
else if(str[i]>=’0′ && str[i]<=’9′) false
else true as str[7]=’;’
specialCharacters++ hence specialCharacters=1
Hence output is consonants=2, vowels=2, digits=2, spaces=1, specialCharacters=1 for string code 21;
Program
import java.util.*; class Count { public static void main(String args[]) { int i,vowels=0,consonants=0,digits=0,spaces=0,specialCharacters=0; String s; Scanner sc = new Scanner(System.in); System.out.println("Enter sentence"); s=sc.nextLine(); char str[] = s.toCharArray(); int n= str.length; for(i=0;i<n;i++) { if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||str[i]=='o' || str[i]=='u' || str[i]=='A' ||str[i]=='E' || str[i]=='I' || str[i]=='O' ||str[i]=='U') { vowels++; } else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z')) { consonants++; } else if(str[i]>='0' && str[i]<='9') { digits++; } else if (str[i]==' ') { spaces++; } else { specialCharacters++; } } System.out.println("Vowels = "+vowels); System.out.println("Consonants = "+consonants); System.out.println("Digits = "+digits); System.out.println("White spaces = "+spaces); System.out.println("Special characters = "+specialCharacters); } }