Total Number Of Vowels In a String : Java Program Code with Example

Vowel need no introduction . But reminding again there are five vowels in the 26 alphabets english that is
' a ' ,' e ' ,' i ' ,' o ' ,' u ' .
To calculate the number of vowels  we need to compare the each character of the string to all the vowels one by one . If the character is equal to one of the vowel then the counter is increased by one .
for example :
if the original string : " Alive is awesome "
then the number of vowels is : 8
Here we pass the string  to the vowelcount() method ,which return the number of vowels in the string.





Demo :

vowel count in java program code





















Please find the code below :


public class VowelsCount
{
    static int i,c,res;
    
    static int vowelcount(String s)
    {
        for(i=0,c=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
            c++;
        }
        return c ;
    }
    
    public static void main (String args[])
    {
        
        System.out.println("Original String is : ");
        System.out.println("   manchester united is also known as red devil   ");
        res=VowelsCount.vowelcount("   manchester united is also known as red devil ");
        System.out.println("The number of vowels in the string is  :" + res);
    }
}



About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry