-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountVowelsConsonants.java
More file actions
28 lines (26 loc) · 937 Bytes
/
CountVowelsConsonants.java
File metadata and controls
28 lines (26 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package Assignment1;
import java.util.Scanner;
public class CountVowelsConsonants {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String: ");
String str = sc.nextLine();
int[] counts = countVowelsConsonants(str.toLowerCase());
System.out.println("Number of vowels: " + counts[0]);
System.out.println("Number of consonants: " + counts[1]);
}
public static int[] countVowelsConsonants(String str) {
int[] counts = new int[2];
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
counts[0]++;
} else {
counts[1]++;
}
}
}
return counts;
}
}