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

A4 Java Programs Class11 Part1

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 reverses the input string and compares it to the original, while the vowel and consonant counter iterates through the input sentence to tally the counts. Sample outputs are provided for both programs demonstrating their functionality.

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

A4 Java Programs Class11 Part1

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 reverses the input string and compares it to the original, while the vowel and consonant counter iterates through the input sentence to tally the counts. Sample outputs are provided for both programs demonstrating their functionality.

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();
}
}

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

You might also like