Program to find total no of vowels in a string

Simple java program to find total number of vowels in a particular string. This type of question are asked in interviews, mainly in technical round, to test the programming skills. This program also helps to find the consonants.

class totalvowels
{
  public static void main(String args[])
    {
	String str="astronaut";

	str.toLowerCase();

	int count=0;
	char myc;
	int len=str.length();

	for(int j=0;j <len;j++)
	  {
	  myc=str.charAt(j);

	  if(myc=='a' || myc=='e' || myc=='i' || myc=='o' || myc=='u')

	     count++;
	  }
	  System.out.println(count);

     }
 }

 

Following Result will be displayed for the above program: 

4

Explanation:
 1 st separate each character in a string, to check whether it a vowel or not.
 If the character is vowel increment count variable.