Java Vowel Count in String Program

By | October 24, 2019

Task: Write a Java program to count vowels in a given string input by the user at runtime.

Java Vowel Count Program

Java Vowel Count Program

The Source Code of Java Vowel Count Program

/*
 * Write down a Java program to input a string.
 * And count number of Vowels in it.
*/
package countvowelsinstring;

import java.util.Scanner;

/**
 *
 * @author www.EasyCodeBook.com
 */
public class CountVowelsInString {

    public static void main(String[] args) {
      int vowelCount = 0;
      System.out.print("Enter a string to count Vowels : ");
      Scanner input = new Scanner(System.in);
      String s1 = input.nextLine();

      for (int i=0 ; i<s1.length(); i++){
         char ch = s1.charAt(i);
         if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'
              ||ch == 'A' || ch == 'E'|| ch == 'I' ||ch == 'O' ||ch == 'U')
            vowelCount ++;
         
      }
      System.out.println("Total number of vowels in the given string is " +vowelCount);
    }
    
}

The output of String Vowel counting Program

Enter a string to count Vowels : hello JAVA programming world
Total number of vowels in the given string is 8

Enter a string to count Vowels : HOW ARE you dear?
Total number of vowels in the given string is 7

Enter a string to count Vowels : Computer Programming Tutorials in Java
Total number of vowels in the given string is 13

Leave a Reply

Your email address will not be published. Required fields are marked *