Count vowels, consonants, digits and other characters in Java

import java.util.Scanner;

public class question1{
	public static void main(String[]args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a string: ");
		String line = input.nextLine();
		int cons = 0, vowe = 0, digi = 0, othr = 0, coun = 0;
		for(char c: line.toCharArray()){
			coun ++;
			if(Character.isLetter(c)){
				switch(c){
					case 'a': case 'e': case 'i': case 'o':	case 'u':
					case 'A': case 'E': case 'I': case 'O':	case 'U':
						vowe ++;
						break;
					default:
						cons ++;
				}
			} else if(Character.isDigit(c)){
				digi ++;
			} else {
				othr ++;
			}
		}
		System.out.printf(
			"%d consonants.%n%d vowels.%n%d numbers.%n%d other characters.%n%d total characters.%n"
			, cons, vowe, digi, othr, coun
		);
	}
}
Download

Comments

Popular posts from this blog