In this article, we'll introduce beginners to a Java program that counts the number of vowels and consonants in a given string.
Basic Concept
English vowels are: A, E, I, O, U (and their lowercase counterparts). Every other letter of the alphabet is typically considered a consonant. By analyzing each character of a string, we can categorize them as vowels or consonants.
Java Program to Count Vowels and Consonants in a String
import java.util.Scanner;
public class VowelConsonantCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your string:");
String input = scanner.nextLine();
int[] counts = countVowelsAndConsonants(input);
System.out.println("Number of vowels: " + counts[0]);
System.out.println("Number of consonants: " + counts[1]);
}
public static int[] countVowelsAndConsonants(String input) {
int vowelsCount = 0, consonantsCount = 0;
for (char c : input.toLowerCase().toCharArray()) {
if (c >= 'a' && c <= 'z') { // Ensure character is a letter
if ("aeiou".indexOf(c) != -1) {
vowelsCount++;
} else {
consonantsCount++;
}
}
}
return new int[]{vowelsCount, consonantsCount};
}
}
Output:
Enter your string:
Hello World
Number of vowels: 3
Number of consonants: 7
Step by Step Explanation:
Taking User Input:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your string:");
String input = scanner.nextLine();
We initiate the Scanner class to collect a string input from the user.
Analyzing Each Character:
for (char c : input.toLowerCase().toCharArray()) {
We convert the entire string to lowercase to make the comparison case-insensitive and then convert the string to a character array. Now, we can easily loop through each character.
Checking if a Character is a Letter:
if (c >= 'a' && c <= 'z') {
Before categorizing a character as a vowel or consonant, we ensure it's a letter of the alphabet.
Identifying Vowels and Consonants:
if ("aeiou".indexOf(c) != -1) {
vowelsCount++;
} else {
consonantsCount++;
}
For each character, if it's found in the string "aeiou", then it's a vowel; otherwise, it's a consonant.
Displaying the Result:
System.out.println("Number of vowels: " + counts[0]);
System.out.println("Number of consonants: " + counts[1]);
After processing the entire string, we display the total count of vowels and consonants.
Conclusion
By creating a program to count vowels and consonants, beginners not only practice string manipulation in Java but also gain exposure to conditional logic and loops. As you further progress on your coding journey, always remember that the foundational understanding you gain from simple exercises like this will be invaluable in building complex applications. Keep practicing and exploring!
Related Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
Comments
Post a Comment
Leave Comment