java - Count vowels in a word -
i need find count of vowels in word. however, when compare letters in word whether vowel or not,
for example, 1 below,
if( word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'u'
...... ) //the rest omitted
the if
statement gets long. there way compare them regex or regex-like comparison , give me number of vowel occurrences in string?
if want find vowels
string line = "ahis order placed qt3000! ok?"; string pattern = "(?i)[aeiou]"; pattern r = pattern.compile(pattern); matcher m = r.matcher(line); while (m.find()) { system.out.println(m.group(0) ); }
if want find number of times occur can use replace
like
string line = "ahis order placed qt3000! ok?"; string pattern = "(?i)[aeiou]"; system.out.println(line.replaceall(pattern, "").length());
note :- (?i)
inline modifier indicates whatever pattern follows case insensitive
Comments
Post a Comment