6
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");
}
}
