Write a Java program to accept a string from the console and count number of Vowels, Consonants, Digits, Tabs and Blank Spaces in a string.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Vowels {
public static void main(String args[]) throws IOException {
String str;
int vowels = 0, consonants = 0, digits = 0, tabs = 0, blanks_spaces = 0, other = 0;
char ch;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String : ");
str = br.readLine();
for(int i = 0; i < str.length(); i ++) {
ch = str.charAt(i);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' ||
ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
{
vowels ++;
}
else if(ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' ||
ch == '6' || ch == '7' || ch == '8' || ch == '9')
{ // You can use "Character.isDigit(ch)" condition
digits ++;
}
else if(ch == '\t') // Or you can use "Character.isWhitespace(ch)"
{
tabs ++;
}
else if(ch == ' ')
{
blanks_spaces++;
}
else if(Character.isAlphabetic(ch))
{
consonants++;
}
else
{
other++;
}
}
System.out.println("No. of Vowels : " + vowels);
System.out.println("No. of consonants : " + consonants);
System.out.println("No. of Digits : " + digits);
System.out.println("No. of Tabs : " + tabs);
System.out.println("No. of Blank Spaces : " + blanks_spaces);
System.out.println("No. of other characters : " + other);
}
}
Enter a String : This program is easy 2 understand !
No. of Vowels : 9
No. of consonants : 18
No. of Digits : 1
No. of Tabs : 0
No. of Blank Spaces : 6
No. of other characters : 1