Programming Tutorials

(追記) (追記ここまで)

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

By: Paawan Chaudhary in Java Tutorials on 2012年09月19日 [フレーム]

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



(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /