There are several ways in which you can count all the vowels of a string. But first we should determine what kind of vowels the text could have. For example, if you should handle accented vowels or not, and what types of accents, in Spanish, in French, in Polish ... According to that, you would have to include in the evaluation the possibility for those vowels.
Here I show you several possible ways to do it for a general count of vowels.
If you also want to count each vowel separately, see part II of the answer.
VIEW FULL DEMO ON REXTESTER
I. Voice count in general
Form 1: with regular expressions
This sentence will find all the Castilian vowels, uppercase or lowercase, within a string:
int totalVocales = s.replaceAll("[^AEIOUaeiouÁÉÍÓÚáéíóú]","").length();
We put it in a method, and we test it:
Method contarVocales
:
public static int contarVocales(String s){
int totalVocales = s.replaceAll("[^AEIOUaeiouÁÉÍÓÚáéíóú]","").length();
return totalVocales;
}
Test:
String helloWorld="Hello world áéíóÚ";
totalVocales=contarVocales(helloWorld);
System.out.println(totalVocales);
Output:
8
Form 2: With a cycle for
Here I have not included the possible vowels in Spanish (accented), but they could be included.
public static int contarVocalesFor(String s){
int totalVocales = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i')
|| (c == 'O') || (c == 'o') || (c == 'U') || (c == 'u'))
totalVocales++;
}
return totalVocales;
}
Test:
String loremIpsum=
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."+
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. "+
"Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "+
"Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
totalVocales=contarVocalesFor(loremIpsum);
System.out.println(totalVocales);
Output:
162
II. General and partial count
We will adapt the method so that it returns us a HashMap
with how many vowels there are of each one and a general count.
public static HashMap<String,Integer> contarVocalesMap(String s){
HashMap<String,Integer> mapTotal=new HashMap<String,Integer>();
int totalVocales = s.replaceAll("[^AEIOUaeiouÁÉÍÓÚáéíóú]","").length();
int totalA = s.replaceAll("[^AaÁá]","").length();
int totalE = s.replaceAll("[^EeÉé]","").length();
int totalI = s.replaceAll("[^IiÍí]","").length();
int totalO = s.replaceAll("[^OoÓó]","").length();
int totalU = s.replaceAll("[^UuÚú]","").length();
mapTotal.put("a",totalA);
mapTotal.put("e",totalE);
mapTotal.put("i",totalI);
mapTotal.put("o",totalO);
mapTotal.put("u",totalU);
mapTotal.put("all",totalVocales);
return mapTotal;
}
We tried the method:
HashMap<String,Integer> mapTotalVocales=contarVocalesMap(loremIpsum);
System.out.println(mapTotalVocales);
Output:
You will have a map like this, with general information and by vowels, it would be a matter of reading it:
{all=162, a=28, e=38, u=29, i=42, o=25}
NOTES:
The methods can still be improved, by making them nullsafe .
We could even think of a Clase
with a much broader scope, having several methods such as: getTotal()
for all vowels, getOne()
for a given vowel, getOpen()
to get only the open vowels. .. Get only the vowels in capital letters, with accents, without accents ... and everything we want with the vowels. :)