Define a class to accept a string. Check if it is a Special String or not. A String is Special if the number of vowels equals to the number of consonants…

ICSE Class 10 Computer Applications PYQ · 2025 Improvement · Section B

6Written · 2025 · Section B · 2025 Improvement

Define a class to accept a string. Check if it is a Special String or not.
A String is Special if the number of vowels equals to the number of consonants.
Example: MINE
Number of vowels = 2
Number of Consonants = 2

Show SolutionHide Solution↓
import java.util.Scanner;

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

        System.out.print("Enter a string: ");
        String str = sc.nextLine().toUpperCase();

        int vowels = 0, consonants = 0;
        int len = str.length();

        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                if (ch == 'A' 
                    || ch == 'E' 
                    || ch == 'I' 
                    || ch == 'O' 
                    || ch == 'U')
                    vowels++;
                else
                    consonants++;
            }
        }

        System.out.println("Number of vowels = " + vowels);
        System.out.println("Number of consonants = " + consonants);

        if (vowels == consonants)
            System.out.println("It is a Special String");
        else
            System.out.println("It is not a Special String");
    }
}
BlueJ output of SpecialString.java BlueJ output of SpecialString.java
← Previous questionNext question →

Browse the free Computer Applications sample bank