public class Vowels {
public static int[] vowelCount(String s) {
int[] ret = new int[5];
int i=0;
int c=' ';
String temp="";
int A = (int)'A';
int E = (int)'E';
int I = (int)'I';
int O = (int)'O';
int U = (int)'U';
temp = s.toUpperCase();
for(i=0; i<5; i++)
ret[i] = 0; // no vowels found
for (i = 0; i < s.length(); i++) {
c = (int)temp.charAt(i);
if (c == A) ret[0]++;
else if (c == E) ret[1]++;
else if (c == I) ret[2]++;
else if (c == O) ret[3]++;
else if (c == U) ret[4]++;
}
return ret;
}
public static void main(String[] args) {
int i = 0;
int[] vc = new int[5];
vc = vowelCount("I think, therefore I am");
for (i=0; i<5; i++)
System.out.println("vowel count[" + i + "] = " + vc[i]);
}
}
|