mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
@ -1,19 +1,16 @@
|
|||||||
package com.thealgorithms.strings;
|
package com.thealgorithms.strings;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vowel Count is a system whereby character strings are placed in order based
|
* 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
|
* on the position of the characters in the conventional ordering of an
|
||||||
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
|
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
|
||||||
*/
|
*/
|
||||||
class CheckVowels {
|
public class CheckVowels {
|
||||||
|
private static final Set<Character> VOWELS = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a string is has vowels or not
|
* Check if a string is has vowels or not
|
||||||
@ -22,11 +19,7 @@ class CheckVowels {
|
|||||||
* @return {@code true} if given string has vowels, otherwise {@code false}
|
* @return {@code true} if given string has vowels, otherwise {@code false}
|
||||||
*/
|
*/
|
||||||
public static boolean hasVowels(String input) {
|
public static boolean hasVowels(String input) {
|
||||||
if (input.matches("[AEIOUaeiou]")) {
|
return countVowels(input) > 0;
|
||||||
countVowels(input);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,20 +27,16 @@ class CheckVowels {
|
|||||||
*
|
*
|
||||||
* @param input a string prints the count of vowels
|
* @param input a string prints the count of vowels
|
||||||
*/
|
*/
|
||||||
public static void countVowels(String input) {
|
public static int countVowels(String input) {
|
||||||
input = input.toLowerCase();
|
if (input == null) {
|
||||||
int count = 0;
|
return 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++;
|
int cnt = 0;
|
||||||
}
|
for (char c : input.toLowerCase().toCharArray()) {
|
||||||
System.out.println(count);
|
if (VOWELS.contains(c)) {
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cnt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user