Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
Vowel and Consonant Program in Java
In computer programming, understanding the fundamentals of language is essential. Just as in human language, where letters can be categorized as either vowels or consonants, we can apply similar principles to programming to work with text data effectively. In this tutorial, we will learn to write Java program to check consonants and vowels!
What are Vowels and Consonants?
Before we dive into the programming aspect, let's clarify what vowels and consonants are:
-
Vowels: Vowels are a set of letters in the English alphabet that are known for their open and sonorous sounds. In English, these letters are 'A,' 'E,' 'I,' 'O,' and 'U.' They are often used in forming the core of words and play a crucial role in pronunciation.
- Consonants: Consonants are the remaining letters in the English alphabet that are not vowels. These letters are characterized by their closure or partial closure, which obstructs the airflow during speech. Consonants provide structure and articulation to words.
Use Cases of Consonants and Vowels in Java Programming
Now that we have a basic understanding of what consonants and vowels are, let's explore some practical applications for working with them in programming:
-
Text Analysis: Analyzing text data often involves counting and categorizing consonants and vowels. This can be useful for tasks such as text summarization, sentiment analysis, and language identification.
-
Word Games: If you're developing word games like Hangman or Scrabble, understanding the distribution of vowels and consonants in words is crucial for gameplay and scoring.
-
Encryption: Some encryption algorithms may modify vowels and consonants differently to enhance security. Understanding their distribution can help in encryption and decryption processes.
-
Language Processing: In natural language processing (NLP) tasks, identifying and processing vowels and consonants can assist in various language-related tasks, including speech recognition and machine translation.
Now, let’s get started and learn vowel program in Java, consonant program, and more with examples and code explanations.
Concepts to Learn:
Vowels Program in Java
Here's a simple Java program to check vowels in a given string, along with an example:
Code
import java.util.Scanner;
public class VowelsProgram {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a string
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Convert the input string to lowercase for case-insensitive matching
input = input.toLowerCase();
// Initialize a counter to keep track of the number of vowels
int vowelCount = 0;
// Loop through each character in the input string
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
// Check if the character is a vowel (a, e, i, o, or u)
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
}
}
// Close the scanner
scanner.close();
// Display the result
System.out.println("Number of vowels in the input string: " + vowelCount);
}
}
Output
Enter a string: Tutorials Freak
Number of vowels in the input string: 6
Explanation
-
We import the java.util.Scanner class to read user input.
-
We prompt the user to enter a string and store it in the input variable.
-
To make the program case-insensitive, we convert the input string to lowercase using input = input.toLowerCase();.
-
We initialize a variable vowelCount to keep track of the number of vowels.
-
We use the Java for loop to iterate through each character in the input string.
-
Inside the loop, we check if the current character ch is a vowel (i.e., 'a', 'e', 'i', 'o', or 'u') and increment vowelCount if it is.
-
After processing the entire string, we close the scanner.
-
Finally, we display the number of vowels found in the input string.
Vowel and Consonant Program in Java Using if-else
Here's a Java program to check whether a given alphabet is a vowel or a consonant using an if-else statement.
Code
import java.util.Scanner;
public class VowelConsonantCheck {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a character
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
// Check if the character is an alphabet (a-z or A-Z)
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
// Convert the character to lowercase for case-insensitive matching
ch = Character.toLowerCase(ch);
// Check if the character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
} else {
System.out.println(ch + " is not an alphabet.");
}
// Close the scanner
scanner.close();
}
}
Output
Enter a character: V
v is a consonant.
Explanation
-
We import the java.util.Scanner class to read user input.
-
We prompt the user to enter a character and store it in the variable ch. We use scanner.next().charAt(0) to read the first character entered by the user.
-
We check if the entered character is an alphabet by verifying if it falls within the ASCII range for lowercase ('a' to 'z') or uppercase ('A' to 'Z') letters.
-
If the character is an alphabet, we convert it to lowercase using ch = Character.toLowerCase(ch); to make the matching case-insensitive.
-
We then use the Java if-else statement to determine whether the character is a vowel ('a', 'e', 'i', 'o', 'u') or a consonant. If it's a vowel, we print that it's a vowel; otherwise, we print that it's a consonant.
-
If the character is not an alphabet, we print that it's not an alphabet.
Check Given Alphabet is Vowel or Consonant in Java Using switch
Here's a Java program to check whether a given alphabet is a vowel or a consonant using a switch statement.
Code
import java.util.Scanner;
public class VowelConsonantCheckSwitch {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a character
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
// Convert the character to lowercase for case-insensitive matching
ch = Character.toLowerCase(ch);
// Check if the character is an alphabet (a-z)
if (ch >= 'a' && ch <= 'z') {
// Use a switch statement to check if the character is a vowel
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(ch + " is a vowel.");
break;
default:
System.out.println(ch + " is a consonant.");
}
} else {
System.out.println(ch + " is not an alphabet.");
}
// Close the scanner
scanner.close();
}
}
Output
Enter a character: U
u is a vowel.
Explanation
-
We import the java.util.Scanner class to read user input.
-
We prompt the user to enter a character and store it in the variable ch. We use scanner.next().charAt(0) to read the first character entered by the user.
-
We convert the character to lowercase using ch = Character.toLowerCase(ch); to make the matching case-insensitive.
-
We check if the entered character is an alphabet by verifying if it falls within the ASCII range for lowercase letters ('a' to 'z').
-
If the character is an alphabet, we use a switch statement to check if the character is a vowel or a consonant. If it's a vowel, we print that it's a vowel; otherwise, we print that it's a consonant. The Java break statement is used to exit the switch statement after a match is found.
-
If the character is not an alphabet, we print that it's not an alphabet.
Print Vowels in String in Java
Here's a Java program to print all the vowels in a given string:
Code
import java.util.Scanner;
public class VowelsInString {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a string
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Convert the input string to lowercase for case-insensitive matching
input = input.toLowerCase();
// Print the vowels in the string
System.out.print("Vowels in the string: ");
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (isVowel(ch)) {
System.out.print(ch + " ");
}
}
// Close the scanner
scanner.close();
}
// Function to check if a character is a vowel
public static boolean isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
}
Output
Enter a string: Welcome to Tutorials Freak!
Vowels in the string: e o e o u o i a e a
Explanation
-
We import the java.util.Scanner class to read user input.
-
We prompt the user to enter a string and store it in the variable input.
-
We convert the input string to lowercase using input = input.toLowerCase(); to make the matching case-insensitive.
-
We define a function isVowel that checks if a character is a vowel by comparing it to the characters 'a', 'e', 'i', 'o', or 'u'.
-
In the main part of the program, we loop through each character in the input string using a for loop in Java.
-
For each character, we use the isVowel function to check if it is a vowel. If it is, we print the character.
-
This program prints all the vowels found in the input string, separated by spaces.
Learn Next: