Count number of vowels, consonants and digits in a String in Java

By: Paawan Chaudhary Viewed: 185757 times  Printer Friendly Format    


This java program accepts a string from the console and counts number of vowels, consonants, digits, tabs and blank spaces in a string.

import java.io.*;
class q5vowels
{
	public static void main(String args[]) throws IOException
	{
		String str;
		int vowels = 0, digits = 0, blanks = 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(Character.isDigit(ch))
				digits ++;
			else if(Character.isWhitespace(ch))
				blanks ++;
		}

		System.out.println("Vowels : " + vowels);
		System.out.println("Digits : " + digits);
		System.out.println("Blanks : " + blanks);
	}
}

Output:

Enter a String : ABC DE 123
Vowels : 2
Digits : 3
Blanks : 2


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Its really helpfull to me thanks................
View Tutorial          By: AKS at 2013-08-10 06:27:43

2. This is the help full sites for diploma student
View Tutorial          By: Tanuja at 2013-08-11 09:41:55

3. Very Great thanks for the code
View Tutorial          By: Alvin Funelas at 2014-07-12 00:42:01

4. my query is like this: " accept a string, cou
View Tutorial          By: samtopper at 2014-07-31 17:40:25

5. why are we using it ch = str.charAt(i);
View Tutorial          By: Rumman at 2014-10-10 19:42:42

6. This has tread error , copy and paster hole code a
View Tutorial          By: Daniel at 2015-01-21 16:24:36

7. thanx very much...
View Tutorial          By: Rajesh at 2015-10-09 08:57:36

8. I am not using readbuffer in the code instead m us
View Tutorial          By: Suhasini P at 2015-11-28 14:18:55

9. You saved my life
View Tutorial          By: Siyabulela Khumalo at 2016-05-18 07:16:21

10. I think something will be wrong..
View Tutorial          By: Alok at 2017-06-17 18:01:38

11. good....
View Tutorial          By: krithvika at 2017-08-12 01:24:15