Solution #1:
- package com.javatutorial;
- public class CountNumberofVowels {
- public static void main(String[] args) {
- System.out.println("Please enter a String");
- Scanner in = new Scanner(System.in);
- String input = in.nextLine();
- char[] Chararray = input.toCharArray();
- int count = 0;
- for (char ch : Chararray) {
- switch (ch) {
- case 'a':
- case 'e':
- case 'i':
- case 'o':
- case 'u':
- case 'A':
- case 'E':
- case 'I':
- case 'O':
- case 'U':
- count++;
- break;
- default:
- }
- }
- System.out.println("Number of vowels in String "+count);
- }
- }
Output:
- Please enter a String
- Instance Of Java
- Number of vowels in String 6
- package com.javatutorial;
- public class CountNumberofVowels {
- public static void main(String[] args) {
- int vowels = 0, digits = 0, blanks = 0; char ch;
- System.out.println("Please enter a String");
- Scanner in = new Scanner(System.in);
- String testString= in.nextLine();
- for(int i = 0; i < testString.length(); i ++)
- {
- ch = testString.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);
- }
- }
- Please enter a String
- vowels and consonants
- Vowels : 6
- Digits : 0
- Blanks : 2
No comments