-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsonantcounter.java
More file actions
30 lines (22 loc) · 869 Bytes
/
consonantcounter.java
File metadata and controls
30 lines (22 loc) · 869 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
29
30
//this program counts the number of consonants in the string given by user
import java.util.Scanner;
public class consonantcounter {
public static int countConsonants(String str) {
int count = 0;
str = str.toLowerCase(); // Convert to lowercase for case-insensitive counting
for (char ch : str.toCharArray()) {
if (Character.isLetter(ch) && !"aeiou".contains(String.valueOf(ch))) {
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
int consonantCount = countConsonants(input);
System.out.println("The number of consonants in the string: " + consonantCount);
scanner.close(); // Close the Scanner to avoid resource leaks
}
}