Dremendo Tag Line

Input sentence and count total number of vowels present in it in Java

String - Question 1

In this question, we will see how to input a sentence and count the total number of vowels present in it in Java programming. To know more about string click on the string lesson.

Q1) Write a program in Java to input a sentence and count the total number of vowels present in it.

Program

import java.util.Scanner;

public class Q1
{
    public static void main(String args[])
    {
        String s;
        char ch;
        int vc=0,i;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a sentence");
        s=sc.nextLine();

        for(i=0; i<s.length(); i++)
        {
            ch=Character.toLowerCase(s.charAt(i));
            if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
            {
                vc++;
            }
        }
        System.out.println("Total vowels = "+vc);
    }
}

Output

Enter a sentence
I am learning Java
Total vowels = 7
video-poster