Answers for "Java Program to print number of vowels and consonants in String"

2

count vowels in a string java

// There are many ways to do it the one i came up with is this:  
int count = 0;
String sentence = "My name is DEATHVADER, Hello World!!! XD";
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char vowel : vowels) 
  for (char letter : sentence.toLowerCase().toCharArray()) 
    if (letter == vowel) count++;
System.out.println("Number of vowels in the given sentence is " + count);

// I have found another easier method if you guyz don't understand this
// but this is also easy xD.
// here you can see another method:
// https://www.tutorialspoint.com/Java-program-to-count-the-number-of-vowels-in-a-given-sentence
Posted by: Guest on May-15-2021
0

calculate the number of vowels and consonants in a string in java

for (int k = 0; k < text.length(); k++) {
    char c = text.charAt(k);
    if (c == 'a' || c == 'e' || c == 'i' || 
       c == 'o' || c == 'u') 
              owls += vowls
else
            consonts += consonts
}
System.out.println("Vowel count is " + vowls); 
System.out.println("Consonant count is: " + consonts);
Posted by: Guest on June-29-2021

Code answers related to "Java Program to print number of vowels and consonants in String"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language