0% found this document useful (0 votes)
0 views2 pages

Full 20 A4 Java Programs NoLabel

The document contains two Java programs: one for checking if a string is a palindrome and another for counting vowels and consonants in a sentence. The palindrome checker compares the input string with its reversed version, while the vowel and consonant counter iterates through the input sentence to tally the counts. Example outputs include 'madam is a palindrome' and 'Vowels: 3, Consonants: 7'.

Uploaded by

4010vandana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Full 20 A4 Java Programs NoLabel

The document contains two Java programs: one for checking if a string is a palindrome and another for counting vowels and consonants in a sentence. The palindrome checker compares the input string with its reversed version, while the vowel and consonant counter iterates through the input sentence to tally the counts. Example outputs include 'madam is a palindrome' and 'Vowels: 3, Consonants: 7'.

Uploaded by

4010vandana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like