G
George W. Cherry
Chris Uppal said:George said:int[] count = countVowelsConsonantsOthers(str);
System.out.println(str);
System.out.println( "Number of vowels = " + count[0] );
System.out.println( "Number of consonants = " + count[1] );
System.out.println( "Number of others = " + count[2] );
Now that /is/ execrable.
I'm sorry but there's no more polite word for it. I realise that you are
only
exploring the options available to you, rather than seriously proposing
this as
the best solution (as least I /hope/ that's the case ;-), but this passes
beyond sane program design.
That bad, huh? Here it is for the Java Hall of Insane Shame.
public static int[] countVowelsConsonantsOthers(String str) {
int vowels = 0, consonants = 0, others = 0;
for (int j = 0; j < str.length(); j = j + 1) {
char ch = str.charAt(j);
if ( Character.isLetter(ch) ) {
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
vowels++;
break; //end cases for vowels
default:
consonants++;
//end ch is a letter but not a vowel
}//end switch statement
}
else { //ch is not a letter
others++;
}
}//end for loop
assert vowels + consonants + others == str.length();
return new int[]{vowels, consonants, others};
}//end countVowelsConsonantsOthers method
George