A4 Java Programs Class11 Part1
A4 Java Programs Class11 Part1
Sample Output:
Input: madam
madam is a palindrome.
2. Count Vowels and Consonants
import java.util.Scanner;
public class VowelConsonantCounter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
int vowels = 0, consonants = 0;
sentence = sentence.toLowerCase();
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (Character.isLetter(ch)) {
if ("aeiou".indexOf(ch) != -1)
vowels++;
else
consonants++;
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
sc.close();
}
}
Sample Output:
Input: Hello World
Vowels: 3
Consonants: 7