Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string.
import java.util.Scanner;
public class vowelconsonents {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string:");
String s = sc.nextLine();
int vowels = 0;
int conso = 0;
for (int i = s.length() - 1; i >= 0; i--) {
switch (s.toLowerCase().charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
case ' ':
break;
default:
conso++;
break;
}
}
System.out.println("The number of vowels is " + vowels);
System.out.println("The number of consonants is " + conso);
}
}
Result
Enter a string: Programming is fun
The number of vowels is 5
The number of consonants is 11
import java.util.Scanner;
public class vowelconsonents {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string:");
String s = sc.nextLine();
int vowels = 0;
int conso = 0;
for (int i = s.length() - 1; i >= 0; i--) {
switch (s.toLowerCase().charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
case ' ':
break;
default:
conso++;
break;
}
}
System.out.println("The number of vowels is " + vowels);
System.out.println("The number of consonants is " + conso);
}
}
Result
Enter a string: Programming is fun
The number of vowels is 5
The number of consonants is 11
No comments:
Post a Comment