Created
September 8, 2020 14:25
-
-
Save TheAlienMann/185774eaeac687305a856b065aee04a5 to your computer and use it in GitHub Desktop.
print out the first n vowels in a given string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static int countMatches(Matcher matcher) { | |
int counter = 0; | |
while (matcher.find()) | |
counter++; | |
return counter; | |
} | |
@Override | |
public String firstNVowels(String str, int n) { | |
Pattern pattern = Pattern.compile("(?i)[aeiou]"); | |
Matcher matcher = pattern.matcher(str); | |
System.out.println(String.format("Number of matchers: %d", countMatches(matcher))); | |
if (countMatches(matcher) < n) { | |
return "invalid"; | |
} | |
StringBuilder result = new StringBuilder(); | |
while (matcher.find()) { | |
for (int i = 1; i <= n; i++) { | |
result.append(matcher.group(i)); | |
} | |
} | |
return result.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment