1.
String Palindrome Checker
import java.util.Scanner;
public class StringPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
if (input.equalsIgnoreCase(reversed)) {
System.out.println(input + " is a palindrome.");
} else {
System.out.println(input + " is not a palindrome.");
}
sc.close();
}
}
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();
}
}
Vowels: 3
Consonants: 7