-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVowelCounter.java
More file actions
29 lines (21 loc) · 965 Bytes
/
VowelCounter.java
File metadata and controls
29 lines (21 loc) · 965 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
// WAP in Java to count the number of vowels in a string
import java.util.Scanner;
public class VowelCounter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
int count = 0; // To store the number of vowels
str = str.toLowerCase(); // Convert the string to lowercase to handle both uppercase and lowercase
// letters
for (int i = 0; i < str.length(); i++) { // Loop through each character of the string
char ch = str.charAt(i); // Get the character at position i
// Check if the character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println("Number of vowels: " + count);
sc.close(); // Close the Scanner (good practice)
}
}