-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathNumVowels2.java
34 lines (28 loc) · 1 KB
/
NumVowels2.java
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
31
32
33
34
import java.util.Scanner;
public class NumVowels2 {
public static void main (String[] args) {
// set count = 0
int count = 0;
// Input the string
Scanner input = new Scanner(System.in);
System.out.println("/* ===== Number of Vowels ===== */");
System.out.print("\nEnter the string: ");
String str = input.next();
// Convert input string to lower case
str = str.toLowerCase();
// Create an array of vowels
char[] vowels = new char[]{ 'a', 'e', 'i', 'o', 'u' };
// Run a loop from 0 to string length
for (int i=0; i<str.length(); i++) {
// Check whether the current character exists in the vowels array
for (int j=0; j<5; j++) {
if (str.charAt(i) == vowels[j]) {
count++;
break;
}
}
}
// Print the result
System.out.println("Number of vowels in \"" + str + "\" = " + count);
}
}