character checking
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') { count++;
When i give the above statement it returns number of vowe开发者_运维百科ls 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?
One succint, simple way to do this without doing 10 comparisons:
if ("aeiouAEIOU".indexOf(c) != -1) { count++; }
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++;
}
If you want to get upper or lowercase a simple approach seems to be the following:
Iterate over all the characters in the word and do the following test:
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u'
|| c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
{ count++ };
You can use tests like:
Character.toLowerCase(c) == 'a'
instead.
I tell you one thing, if you give the out put in upper case then in your code accept given out put in lower case.
if string str='FRIEND'
handle str.Tolower(); Please try this I think your problem resoloved.
How about:
if (Arrays.asList('a', 'e', 'i', 'o', 'u').contains(Character.toLowerCase(c))) {
...
}
I would also static final the list as well.
Here is a quite simple utility class :
public class Main {
public static int countChars(String string, Character... characters) {
return countChars(string, new HashSet<Character>(Arrays.asList(characters)));
}
public static int countChars(String string, Set<Character> characters) {
int count = 0;
for(int i = 0; i < string.length(); i++){
if(characters.contains(string.charAt(i))){
count++;
}
}
return count;
}
public static int countCharsIgnoreCase(String string, Character... characters) {
return countCharsIgnoreCase(string, new HashSet<Character>(Arrays.asList(characters)));
}
public static int countCharsIgnoreCase(String string, Set<Character> characters) {
Set<Character> finalCharacters = new HashSet<Character>();
for (Character character : characters) {
finalCharacters.add(Character.toUpperCase(character));
finalCharacters.add(Character.toLowerCase(character));
}
return countChars(string, finalCharacters);
}
}
code on ideone
Not the most efficient solution, but perhaps the shortest? :)
int vowelCount = (inputString+" ").split("(?i)[aoeuiy]").length - 1
Btw, am I the only one counting 'y' as a wovel? ;)
Didn't see a Set
yet :-(
static Character charvowels[] = { 'A','I','U','E','O','a','i','u','e','o' };
static Set<Character> vowels = new HashSet<Character>(Arrays.asList(charvowels));
...
public void countVowel(char c) {
if (vowels.contains(c)) count++;
}
Using Guava's CharMatcher:
private final CharMatcher vowels = CharMatcher.anyOf("aeiouAEIOU");
...
// somewhere in your method:
if (vowels.matches(c)) {
...
}
精彩评论