Instagram | / javac_programming
Here we have two variables vcount and ccount to keep the count of vowels and consonants respectively. We have converted each char of the string to lowercase using toLowerCase() method for easy comparison.
We are then comparing each char of the string to vowels ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ using charAt() method and if..else..if statement, if a match is found then we are increasing the vowel counter vcount else we are increasing the Consonant counter ccount.
public class JavaExample {
public static void main(String[] args) {
String str = "BeginnersBook";
int vcount = 0, ccount = 0;
//converting all the chars to lowercase
str = str.toLowerCase();
for(int i = 0; i _ str.length(); i++) { char ch = str.charAt(i); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vcount++; } else if((ch _= 'a'&& ch _= 'z')) {
ccount++;
}
}
System.out.println("Number of Vowels: " + vcount);
System.out.println("Number of Consonants: " + ccount);
}
}
In questa pagina del sito puoi guardare il video online Java Program to Count Vowels and Consonants in a String della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Aman Tripathi 29 maggio 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 44 volte e gli è piaciuto 3 spettatori. Buona visione!