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

By: Paawan Chaudhary Emailed: 1648 times Printed: 2119 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(7)


1. View Comment

Its really helpfull to me thanks................

View Tutorial          By: AKS at 2013-08-10 06:27:43
2. View Comment

This is the help full sites for diploma student

View Tutorial          By: Tanuja at 2013-08-11 09:41:55
3. View Comment

Very Great thanks for the code

View Tutorial          By: Alvin Funelas at 2014-07-12 00:42:01
4. View Comment

my query is like this: " accept a string, count no of vowels and remove all vowels". then print the string without vowels.

View Tutorial          By: samtopper at 2014-07-31 17:40:25
5. View Comment

why are we using it ch = str.charAt(i);

View Tutorial          By: Rumman at 2014-10-10 19:42:42
6. View Comment

This has tread error , copy and paster hole code and has that after enter any sentence . out of bounds error thread main thing problem ,

View Tutorial          By: Daniel at 2015-01-21 16:24:36
7. View Comment

thanx very much...

View Tutorial          By: Rajesh at 2015-10-09 08:57:36

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Paawan Chaudhary
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number
Java program to print prime numbers using Thread
Java program to calculate volume of a Box
Java program to display numbers from 1 to 10 on Applet
Draw a bar chart in Java

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
wait(), notify() and notifyAll() in Java - A tutorial
XML and Java - Parsing XML using Java Tutorial
Sample Java program shows how to write to COM port using Java.
append() in Java
Integer: byte, short, int, and long data types in Java
Student marks calculation program in Java
Method Overloading (function overloading) in Java
Recursion in java
Stack example in Java - push(), pop(), empty(), search()
FileReader and FileWriter example program in Java
Count number of vowels, consonants and digits in a String in Java
How to use Iterator in Java
Vector example in Java
Using StringTokenizer in Java
Most Emailed Articles (in Java)
What is Multithreaded programming?
TCP Server and TCP Client in Java
wait(), notify() and notifyAll() in Java - A tutorial
for loop in java
Why java is important to the Internet
Read from a COM port using Java program
Update contents of a file within a jar file
Calculate gross salary in Java
Calculate average sale of the week in Java
setPriority() and getPriority() in Java
Window Object
constants and variables
Handling Fractions in Java
HTTPS Java - URL Connection program. Using HttpsURLConnection
Type Casting in Java