Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

character checking

Tags:

java

if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') { count++;

When i give the above statement it returns number of vowels in a word only if the given word is in lowercase (ie: input: friend output 2 vowels). I Want to know even if i give uppercase or mixed it should return number of vowels. How to do it?

like image 416
Sumithra Avatar asked Oct 13 '10 07:10

Sumithra


2 Answers

One succint, simple way to do this without doing 10 comparisons:

if ("aeiouAEIOU".indexOf(c) != -1) { count++; }
like image 140
Grodriguez Avatar answered Oct 28 '22 14:10

Grodriguez


Here there is a complete example - note they turn the string to lower case before checking the vowels.

You can also try with a case insensitive regular expression, example from http://www.shiffman.net/teaching/a2z/regex/:

String regex = "[aeiou]";               
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);   
int vowelcount = 0;
Matcher m = p.matcher(content);         // Create Matcher
while (m.find()) {
  //System.out.print(m.group());
  vowelcount++;
}
like image 28
Dror Avatar answered Oct 28 '22 13:10

Dror