Java Program to Print Vowels in a String
A vowel is a speech sound that is pronounced with an open vocal tract, and is one of the five basic types of speech sound in many languages, including English. In this article, we will learn how to write a Java program to print vowels in a string.
Code Example:
import java.util.Scanner;
public class VowelsInString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u' || str.charAt(i) == 'A' || str.charAt(i) == 'E' || str.charAt(i) == 'I' || str.charAt(i) == 'O' || str.charAt(i) == 'U') {
System.out.print(str.charAt(i) + " ");
}
}
}
}
Explanation:
-
The first step is to import the Scanner class from the java.util package, which is used to read input from the user.
-
Next, we declare a class named VowelsInString which contains the main method.
-
Inside the main method, we create a new Scanner object called input, which is used to read input from the user.
-
We prompt the user to enter a string using the print statement, and assign the input to the variable str using the nextLine() method.
-
Next, we use a for loop to iterate through each character in the string.
-
Inside the for loop, we use an if statement to check if the current character is a vowel. We check for both uppercase and lowercase vowels.
-
If the current character is a vowel, we print it to the console.
-
The program continues to iterate through the string until it has checked all characters.
-
Finally, the program terminates.
This program is a simple and straightforward way to print vowels in a string using Java. You can customize the program to suit your needs and add more functionality.
- Using Regular Expressions to find vowels in a string:
Java provides a built-in package called java.util.regex, which can be used to perform regular expression operations on a string. We can use regular expressions to check if a character is a vowel. Here's an example of how to use regular expressions to find vowels in a string:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VowelsInString {
public static void main(String[] args) {
String str = "This is a sample string";
Pattern pattern = Pattern.compile("[aeiouAEIOU]");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.print(matcher.group() + " ");
}
}
}
Explanation:
-
First we import the Matcher and Pattern classes from the java.util.regex package.
-
In the main method, we create a string called str which contains the text we want to search for vowels.
-
Next, we create a Pattern object by calling the compile() method and passing in the regular expression "[aeiouAEIOU]". This regular expression will match any single character that is a vowel, regardless of case.
-
We then create a Matcher object by calling the matcher() method on the Pattern object and passing in the string we want to search.
-
We use a while loop to iterate through the string, and call the find() method on the Matcher object to check if the next occurrence of a vowel is found.
-
If a vowel is found, the program will print it to the console using the group() method.
-
The program continues to iterate through the string until it has checked all characters.
-
Finally, the program terminates.
-
Counting the number of vowels in a string:
To count the number of vowels in a string, we can use a similar approach as above, but instead of printing the vowels, we will keep a counter variable that increments each time a vowel is found. Here's an example:
public class VowelsInString {
public static void main(String[] args) {
String str = "This is a sample string";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u' || str.charAt(i) == 'A' || str.charAt(i) == 'E' || str.charAt(i) == 'I' || str.charAt(i) == 'O' || str.charAt(i) == 'U') {
count++;
}
}
System.out.println("Number of vowels: " + count);
}
}
Explanation:
-
In the main method, we create a string called str which contains the text we want to search for vowels.
-
We create a variable called count and initialize it to 0.
Popular questions
- How can we find vowels in a string using Java?
Answer: One way to find vowels in a string using Java is to use a for loop to iterate through each character in the string, and check if the character is a vowel using an if statement. Another way is to use regular expressions in the java.util.regex package.
- What is the purpose of the Matcher class in the java.util.regex package?
Answer: The Matcher class in the java.util.regex package is used to perform match operations on a string, using a pre-defined pattern. It can be used to find, replace, and manipulate text in a string.
- How can we count the number of vowels in a string using Java?
Answer: To count the number of vowels in a string using Java, we can use a for loop to iterate through each character in the string, and check if the character is a vowel using an if statement. We can also use regular expressions in the java.util.regex package.
- What is the difference between the find() and matches() methods in the Matcher class?
Answer: The find() method in the Matcher class attempts to find the next occurrence of the pattern in the input string, and returns true if it finds a match. The matches() method, on the other hand, attempts to match the entire input sequence against the pattern, and returns true only if the entire input sequence matches the pattern.
- What is the purpose of the group() method in the Matcher class?
Answer: The group() method in the Matcher class returns the input subsequence matched by the previous match. It can also be used to return specific groups of the match. It can be used in conjunction with the find() method to retrieve the matched group of characters.
Tag
String-manipulation