In this post, we will see a Java program to print vowels in a String. If a String contains any vowels, then we will display those vowels to the user.
Generally, the English alphabet contains 5 vowels a, e, i, o, and u. In programming also, we will find those vowels and print them. So to do this, we have to write the logic for extracting these literals from a String.
Let’s understand the hidden logic here.
Table of Contents
Logic to find vowels in a String
Let’s think.. How we can extract any character from a String in a program?
Simply, we can use the charAt( ) method, right? Yes, we can use the charAt( ) method.
Okay, the next question is how can we get those characters in a String? Simply, If we will iterate String in ascending order or descending order, then easily we can find those characters one by one and to iterate we can use any iterative loop like for loop or while loop.
That’s it!
We can print those characters. We got all the vowels!
Piece Of Code
Algorithm
1. Accept any String from the user.
2. Convert given String to lowercase String.
3. Start for loop from '0' to str.length( )-1
4. Declare if condition to check whether the current character is vowel or not
5. Display vowels of given String.
Code language: Java (java)
Program-1: Java Program to print vowels in a String
In this program, we will see how to find vowels in a String. We will use all vowels directly with for loop and if condition to check them.
Let’s see the program now.
package cf.java.string;
import java.util.Scanner;
public class JavaProgramToPrintVowelsInAString {
public static void main(String[] args) {
String str = null;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any String: ");
str = sc.nextLine();
str = str.toLowerCase();
System.out.print("Vowels in the given String are:");
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') {
System.out.print(" " + str.charAt(i));
}
}
}
}
Code language: Java (java)
Output:
Enter any String: Codingface
Vowels in the given String are: o i a e
Code language: JavaScript (javascript)
Explanation
The program first declares a string variable str
and initializes it to null. This means that the variable does not yet have any value.
Next, the program creates a Scanner object sc
to read input from the user. It prompts the user to enter a string and stores the input in the str
variable.
The next step is to convert the string to lowercase using the toLowerCase()
method. This is because the charAt()
method only works with lowercase letters.
Now, the program enters a for
loop to iterate through each character in the string. In each iteration, the program checks if the current character is a vowel. A vowel is any of the five letters a
, e
, i
, o
, or u
. If the current character is a vowel, the program prints it to the console.
Finally, the program terminates.
Also Read: Palindrome program in Java for both String and Number.
Program-2: Java Program to count the number of vowels & consonants in a String
In the first program, We have already seen that how we can write a Java program to print vowels in a String. Let’s see another program to count the number of vowels and consonants in a String.
In the below program, we will count both vowels and consonants of String and we will display the count to the user.
package cf.java.string;
import java.util.Scanner;
public class JavaProgramToCountVowelsAndConsonants {
public static void main(String[] args) {
String str = null;
int vCount=0,cCount=0;
Scanner sc = new Scanner(System.in);;
System.out.print("Enter any String: ");
str = sc.nextLine();
str = str.toLowerCase();
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') {
vCount++;
}
else if(str.charAt(i)>='a' && str.charAt(i)<='z'){
cCount++;
}
}
System.out.println("No of vowels given in String: "+vCount);
System.out.println("No of consonant given in String: "+cCount);
}
}
Code language: Java (java)
Output:
Enter any String: Codingface
No of vowels given in String: 4
No of consonant given in String: 6
Code language: JavaScript (javascript)
Explanation
The Java program to count vowels and consonants first declares two variables, str
and vCount
. The str
variable is a string to store the input string, and the vCount
variable is an integer to store the number of vowels.
The program then creates a Scanner object to read input from the user. It prompts the user to enter a string and stores the input in the str
variable.
Next, the program converts the string to lowercase using the toLowerCase()
method. This is because the charAt()
method only works with lowercase letters.
Now, the program enters a for
loop to iterate through each character in the string. In each iteration, the program checks if the current character is a vowel. A vowel is any of the five letters a
, e
, i
, o
, or u
. If the current character is a vowel, the program increments the vCount
variable. Otherwise, if the current character is a consonant, the program does nothing.
Finally, the program prints the number of vowels and consonants to the console.
Conclusion
This article has shown how to print vowels in a String in Java and also how to count the no of vowels and consonants in a String. Both of the programs are simple and easy to understand. You can use these programs as a starting point to learn more about how to work with strings in Java.
People Also Search For:
- Write a Java program to print vowels in a String.
- Java program to find vowels in a string
- Program to print vowels in a string in Java
- Program to check vowel in Java
- Vowels program in Java using Scanner
- Write a program to calculate the number of vowels present in the string
- Java program to count number of vowels and consonants in a string
Frequently Asked Questions (FAQs)
How do you print a vowel in a String?
By using the charAt( ) method and for loop or while loop, we can compare with all vowels i.e. a, e, i, o, u, and when we find any of them, we can print that vowel.
What are vowels in English?
Vowels in English are the letters ‘a,’ ‘e,’ ‘i,’ ‘o,’ and ‘u.’ They are known for their distinct sounds and play a crucial role in forming words.
Can we use the same English alphabet vowels in Java?
Yes, we can use the same 5 English alphabet vowels a, e, i, o, and u in Java also.
Which Iterator we can use to print vowels in a String?
We can use any iterators like for loop or while loop to print vowels in a String.
Can I use this program to extract vowels from multiple words in a sentence?
Yes, absolutely! This program can handle sentences as well. Just input your sentence, and it will identify and print the vowels within it.
5 thoughts on “Java Program to Print Vowels in a String – 2 Easy Programs”