You are on page 1of 1

Sub-Subject: Java Programming

Topic: String

Step-by-step

Step 1 of 3:

Palindrome Number: A palindrome string is a word that reads the same forward as
backward. In other words, if the order of the letters is reversed in a palindrome, it will
still be the same word or phrase. Examples of palindromic words are radar, level, and
civic.

Explanation: The forward and reverse order of the palindrome string is same and the
palindrome number also shows the same behaviour.

Step 2 of 3:

Program Plan:

 To create the program first take the input from the user using the Scanner class.
 Create the isPalindrome() function to check that the string is palindrome or not
and use the conditional statement to use it.
 Create the countVowels() and countConsonant() function to count the number of
vowels and consonants in the string. Write the isVowel() function to determine
the character is vowel or consonant.
 Print the computed output.

Explanation: The program plan includes create four important functions namely
isPalindrome(), countVowels() , countConsonant() and isVowel() to produce the desired
output.

Step 3 of 3:

The source code of the program is as follows:


import java.util.Scanner;
public class PalindromeAndCount {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();

if (isPalindrome(inputString)) {
System.out.println("The entered string is a palindrome.");

int vowelCount = countVowels(inputString);


System.out.println("Number of vowels in the string: " + vowelCount);
} else {
System.out.println("The entered string is not a palindrome.");

int consonantCount = countConsonants(inputString);


System.out.println("Number of consonants in the string: " +
consonantCount);
}
}

// Function to check if a string is a palindrome


private static boolean isPalindrome(String str) {
String reversedString = new StringBuilder(str).reverse().toString();
return str.equalsIgnoreCase(reversedString);
}

// Function to count the number of vowels in a string


private static int countVowels(String str) {
int count = 0;
for (char ch : str.toCharArray()) {
if (isVowel(ch)) {
count++;
}
}
return count;
}

// Function to check if a character is a vowel


private static boolean isVowel(char ch) {
return "aeiouAEIOU".indexOf(ch) != -1;
}

// Function to count the number of consonants in a string


private static int countConsonants(String str) {
int count = 0;
for (char ch : str.toCharArray()) {
if (Character.isLetter(ch) && !isVowel(ch)) {
count++;
}
}
return count;
}
}

Explanation: This program defines three functions: isPalindrome, countVowels, and


countConsonants. The main function takes user input, checks if the input string is a
palindrome, and then calculates and displays the appropriate count of vowels or
consonants.

Final Answer:

The source code of the program is given above. After entering the “radar” as input the
program generates the following output.

Enter a string: radar


The entered string is a palindrome.
Number of vowels in the string: 2

You might also like