Java count vowels

This program will count the number of vowels in a String.

Java count vowels

This program will count the number of vowels in a String.

Java count vowels

Java count vowels

In this section you will learn how to count the number of vowels in String. Here you will ask to enter a string of your own choice and then you will get the number of vowels count.

Description of code : In this code first you will ask to enter the string through scanner class and compiler will read the string once you enter the string. Then we have taken a count variable which is initialized to 0. Now we have applied a loop here which will go up to length of string input and inside loop checking each character with if condition for vowel, if it is vowel, count is incremented, prints the no of vowel. to console.

Example  : A Code to count the number of vowel in a string.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Check {

 public static void main(String args[]) throws NumberFormatException, IOException
  {
    System.out.println("Enter the character");
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine();
    int count=0;
    System.out.print("Vowel are = ");
         for(int i=0;i<input.length();i++)
          { 
            char ch=input.charAt(i);
            if(ch=='e'||ch=='o'||ch=='i'||ch=='a'||ch=='u'||ch=='O'||ch=='I'||ch=='E'||ch=='O'||ch=='A')
                        count++;
          }
             System.out.println(count); 
            }
       }

Output : After executing the program

Download SourceCode