KnowledgeBoat Logo
LoginJOIN NOW

Computer Applications

Define a class to accept a string and convert it into uppercase. Count and display the number of vowels in it.

Input: robotics
Output: ROBOTICS
Number of vowels: 3

Java

Java String Handling

ICSE Sp 2024

74 Likes

Answer

import java.util.Scanner;

public class KboatCountVowels
{
    public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        String str = in.nextLine();
        str = str.toUpperCase();
        str += " ";
        int count = 0;
        int len = str.length();
        
        for (int i = 0; i < len - 1; i++) 
        {
            char ch = str.charAt(i);
            if(ch == 'A' 
                || ch == 'E' 
                || ch == 'I'
                || ch == 'O'
                || ch == 'U')
                count++;
        }
        
        System.out.println("String : " + str);
        System.out.println("Number of vowels : " + count);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Define a class to accept a string and convert it into uppercase. Count and display the number of vowels in it. Input: robotics Output: ROBOTICS Number of vowels: 3

Answered By

30 Likes


Related Questions