Write a java program to Count Vowels in a string.

Given a string input, write a program to find the total number of vowels in the given string. It returns an int that corresponds to the total number of vowels in the given string.

Input and Output Format

  • Input consists of a string.
  • The output consists of an integer.

Sample Input:

aei

Sample Output:

3

Count Vowels in a string in Java

Following are the steps to count Vowels in a string:

  • Input string from the user. Pass it to getvalues() method.
  • Inside the method, first, convert it into lowercase. Now, create one more string and initialize it with all the vowels.
  • Now, iterate using for loop and compare characters of both the string. If they are equal, then increment count value.
  • At last, print the value of count.
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();

    getvalues(s1);
  }

  public static void getvalues(String s1) {
    String s2 = s1.toLowerCase();
    String s3 = "aeiou";
    int count = 0;
    for (int i = 0; i < s2.length(); i++) {
      for (int j = 0; j < s3.length(); j++) {
        if (s2.charAt(i) == s3.charAt(j)) {
          count++;
        }
      }
    }
    System.out.println(count);
  }
}

Output

Program to Count Vowels and Consonants in Java

Given a string input, write a program to find the total number of vowels and consonants in the given string. It prints a total number of vowels and consonants in the given string.

Input and Output Format

  • Input consists of a string.
  • The output consists of an integer.

Sample Input:

Codebun

Sample Output:

Vowels : 3

Consonants: 4

Following are the steps to find vowels & consonants in a string:

  • Input string from the user. Pass it to getvalues() method.
  • Inside the method, first, convert it into lowercase. Now, iterate over this string and get each character one by one in variable ‘c’.
  • Next, check if it is equal to any vowels. If it is equal then the increment count of vowels. Next, check if the character is within ‘a’ & ‘z’ then increment the count of consonants.
  • At last, just print the count value.
package com.demo;


import java.util.Locale;
import java.util.Scanner;

public class TestJava2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        getvalues(s1);
    }

    public static void getvalues(String s1) {
        int vowelCount = 0, consonantCount = 0;
        String s2 = s1.toLowerCase();
        for(int i=0;i<s2.length();i++){
            char c = s2.charAt(i);
            if(c=='a' ||c=='e' ||c=='i' ||c=='o' ||c=='u' ){
                vowelCount++;
            }
            else if ((c >= 'a' && c <= 'z')) {
                consonantCount++;
            }
        }
        System.out.println("Vowels: "+vowelCount+"\n"+"Consonants: "+consonantCount);
    }
}

Output

Thus, in this way, we learn how to count a number of vowels & consonants in Java.