Program to find number of each vowel in a string.

Program to find number of each vowel in a string. i.e number of a, e, i, o, u separately in a string using switch case. Couting each or particular word in a string. Asked in interviews, mostly in technical round, to test programming skills. Programming languages like java, php, c++,c, c sharp.

 

class vowels

{

  public static void main(String args[])

    {

	String str="astronaut";

	str=str.toLowerCase();

	char myc;

	int a=0,e=0,i=0,o=0,u=0;

	int len=str.length();
	

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

	      switch(myc)

	        {

	            case 'a':

	              a++;

	              break;

	            case 'e':

	              e++;

	              break;

	            case 'i':

	              i++;

	              break;

	            case 'o':

	              o++;

	              break;

	            case 'u':

	              u++;

	              break;	           
	        }
	  }	    

	     	  System.out.println("Number  of A:-"+a);

	     	  System.out.println("Number  of E:-"+e);

	     	  System.out.println("Number of I:-"+i);

	     	  System.out.println("Number of O:-"+o);

	     	  System.out.println("Number of U:-"+u);
    }
}

 

Output for above program:
Number of A:- 2
Number of E:- 0
Number of I:- 0
Number of O:- 1
Number of U:- 1

Also check the program to find total number of vowels in a string here.