JavaAlgorithms/Strings/CheckVowels.java

52 lines
1.3 KiB
Java
Raw Normal View History

package Strings;
2021-09-23 00:29:16 +08:00
/**
2021-09-23 00:29:51 +08:00
* Vowel Count is a system whereby character strings are placed in order based on the position of
* the characters in the conventional ordering of an alphabet. Wikipedia:
2021-09-23 00:29:16 +08:00
* https://en.wikipedia.org/wiki/Alphabetical_order
*/
2021-09-23 00:29:51 +08:00
class CheckVowels {
public static void main(String[] args) {
assert !hasVowels("This is a strings");
assert hasVowels("Hello World");
assert hasVowels("Java is fun");
assert !hasVowels("123hi");
assert hasVowels("Coding vs Programming");
}
2021-09-23 00:29:16 +08:00
/**
* Check if a string is has vowels or not
*
* @param input a string
* @return {@code true} if given string has vowels, otherwise {@code false}
*/
2021-09-23 00:29:51 +08:00
public static boolean hasVowels(String input) {
if (input.matches("[AEIOUaeiou]")) {
countVowels(input);
return true;
2021-09-23 00:29:16 +08:00
}
2021-09-23 00:29:51 +08:00
return false;
}
/**
* count the number of vowels
*
* @param input a string prints the count of vowels
*/
public static void countVowels(String input) {
input = input.toLowerCase();
2021-09-23 00:29:51 +08:00
int count = 0;
int i = 0;
while (i < input.length()) {
if (input.charAt(i) == 'a'
|| input.charAt(i) == 'e'
|| input.charAt(i) == 'i'
|| input.charAt(i) == 'o'
|| input.charAt(i) == 'u') {
count++;
}
i++;
2021-09-23 00:29:16 +08:00
}
2021-09-23 00:29:51 +08:00
System.out.println(count);
}
}