Java program to count the number of vowels in a String

Program to Count the Number of vowels

Program to Count the Number of vowels we’re going to check how many vowels are present in a given String . There are five vowels in English vocabulary, they are – ‘a’, ‘e’, ‘i’, ‘o’, ‘u’.

A String is entered in this case is “s” variable So, we need to check how many vowels are present in given string

For Example, in the string prepinsta then in that case then vowesl are 3 (a,e,i)

Program count the number of vowels in a string

Algorithm

  • Take a String input from user store in variable called “s” 
  • Convert String to char array using toCharArray() 
  • Start for loop from i=0 to i<s.length()
  • Check condition if s.charAt(i)==’a’ or s.charAt(i)==’e’ or s.charAt(i)==’i’ or s.charAt(i)==’o’ or s.charAt(i)==’o’ to increment vowel++
  • Take else if part s.charAt(i)>=’a’ && s.charAt(i)<=’z’ to increment consonant ++
  • Take another else if part s.charAt(i)>=’1′ && s.charAt(i)<=’9′ to increment digit++
  • Take another else if part s.charAt(i)==’ ‘ to increment whitespaces++
  • Take else part to increment symbols++

Code in Java

Run
// Write a program to calculate the number of vowels present in the string in java
import java.util.Scanner;

public class  Main{

	public static void main(String[] args) {
		String s = "prepinsta";
		char[] c = s.toCharArray();
		int vowel=0;
		
		for (int i = 0; i < s.length(); i++) { 
            if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u') 
                vowel++; 
		}
  
	System.out.println("Vowels: " + vowel);
    }
}

Output

Vowels: 3

Method 2

  • First take the string input in a string variable and make a function countVowels(str,str.length())
  • if the value of n that is (length of string) is 1 then we will return . (This is the base case)
  • if the size of the string is not 1 then countVowels(str, n-1) + isVowel(str[n-1])
  • Here isVowel(str[n-1]) will be called if the character is vowel then this will return 1 otherwise 0
Run
public class Main {
    static int isVowel(char ch) {
        ch = Character.toUpperCase(ch);
        if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
            return 1;
        else
            return 0;
    }
    // to count total number of vowel from 0 to n
    static int countVowels(String str, int n) {
        if (n == 1) return isVowel(str.charAt(n - 1));
        return countVowels(str, n - 1) + isVowel(str.charAt(n - 1));
    }
    // Main Calling Function
    public
    static void main(String args[]) {
        // string object
        String str = "prepinsta";

    // Total numbers of Vowel
    System.out.print("Total number of Vowel = ");
    System.out.println(countVowels(str, str.length()));
  }
}

Output

Total number of Vowel = 3

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

14 comments on “Java program to count the number of vowels in a String”


  • Khushi Saxena

    import java.util.*;
    class HelloWorld {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    int count=0;
    for(int i=0;i<s.length();i++){
    char c=s.charAt(i);
    if(c == 'A' || c == 'E' || c == 'I' || c == '0' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
    count++;
    }
    }
    System.out.print(count);
    }
    }


  • Vivek

    import java.util.Scanner;
    public class String2 {
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    char[] ch = s.toCharArray();
    String v = “AEIOUaeiou”;
    char [] vow = v.toCharArray();
    int count = 0;
    for(int i=0; i< ch.length; i++){
    for(int j=0; j< vow.length; j++){
    if(ch[i] == vow[j]){
    count++;
    }
    }
    }
    System.out.println(count);
    }
    }


  • Gyanendra

    import java.util.*;
    public class Main
    {
    public static void main (String[]args)
    {
    String str=”Gyanendra Tiwari”;

    System.out.println(find(str));
    }

    public static int find(String str){
    int count=0;

    for(int i=0;i<str.length();i++){
    char ch=Character.toLowerCase(str.charAt(i));

    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){
    count++;
    }

    }

    return count;
    }
    }


  • Siddhesh

    import java.util.Scanner;

    public class Vowel {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter the String:”);
    String s = sc.nextLine();
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
    char ch = s.charAt(i);
    if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'a' || ch == 'e' || ch == 'i'
    || ch == 'o' || ch == 'u') {
    count++;
    }
    }
    System.out.println("The number of vowels are in " + s + " is " + count);
    }

    }


  • SOURABH KSHIRSAGAR

    public class Main
    {
    public static void main(String[] args) {
    String s = “prepinsta”;
    char[] c = s.toCharArray();
    int vowel=0;

    for (int i = 0; i < s.length(); i++) {
    if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
    vowel++;
    }

    System.out.println("Vowels: " + vowel);
    }
    }


  • Dipika

    import java.util.*;
    public class Main {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (c == 'A' || c == 'E' || c == 'I' || c == '0' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
    count++;
    }
    }
    System.out.println(count);
    }
    }


  • Sandeep

    public class Main
    {
    public static void main(String[] args) {
    String str=”prepinsta”;
    int count=0;

    for(char ch: str.toCharArray()){
    if(ch==’a’||ch==’e’||ch==’i’||ch==’o’||ch==’u’)
    count++;
    }

    System.out.println(“Number of vowels= “+count);
    }
    }


  • Srinibash

    import java.util.Scanner;
    public class p3 {
    public static void main(String[] args){

    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter a String”);
    String s1 = sc.nextLine();
    int vowel = 0;
    int consonant = 0;
    for(int i = 0 ; i <s1.length() ; i ++){
    if(s1.charAt(i) == 'a' ||s1.charAt(i) == 'e' ||s1.charAt(i) == 'i' ||s1.charAt(i) == 'o' ||s1.charAt(i) == 'u' ||s1.charAt(i) == 'A' ||s1.charAt(i) == 'E' ||s1.charAt(i) == 'I' ||s1.charAt(i) == 'O' ||s1.charAt(i) == 'U' ){
    vowel ++;
    }else{
    consonant ++;
    }
    }
    System.out.println(vowel + " vowels present");
    System.out.println(consonant + "consonants present");
    }
    }


  • Asif Basheer

    import java.util.*;
    class Baba {
    public static void main(String[] arfs) {
    Scanner sc = new Scanner(System.in);
    System.out.print(“Enter a String: “);
    String s = sc.next();
    int count = 0;
    for(int i=0; i<s.length(); i++) {
    char x = Character.toLowerCase(s.charAt(i));

    if(x == 'a' || x == 'e'|| x== 'i'|| x=='o' || x=='u') {
    count++;
    }
    }
    System.out.println(count);
    }
    }


  • Dhivya

    import java.util.Scanner;

    public class vowelcount {
    public static void main(String[] args) {
    int c=0;
    String vowel=”aeiouAEIOU”;
    Scanner sc= new Scanner(System.in);
    String s= sc.next();
    for( int i=0;i< s.length();i++){
    if( vowel.indexOf(s.charAt(i)) !=-1){
    c++;
    }
    }
    System.out.println(c);
    }
    }


  • Abhilash

    package arrays;
    class distinct {
    public static void main(String[] args)
    {
    int count=0;
    String str=”abhiaei”;
    for(int i=0;i<str.length();i++) {
    char ch = str.charAt(i);

    if (ch=='a' || ch=='e' || ch=='i' || ch=='o'|| ch=='u') {

    count++;
    }

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

    }