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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -