Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java String-examples Java count vowels

Related Tutorials


 
 

Share on Google+Share on Google+

Java count vowels

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

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2013

Related Tutorials

Discuss: Java count vowels  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com