Java program to print vowels in a String

In Java, String is represented as an Object.

String characters are represented in Unicode encoding.

In the English language, there are five vowels a, e, i, o, and u.

In any string, vowels can be in small or in large letters.

Here using the Java program we will see how to find vowels in the given string.

if (s.charAt(i) == ‘a’ || s.charAt(i) == ‘e’ || s.charAt(i) == ‘i’ || s.charAt(i) == ‘o’ || s.charAt(i) == ‘u’ || s.charAt(i) == ‘A’ || s.charAt(i) == ‘E’ || s.charAt(i) == ‘I’ || s.charAt(i) == ‘O’ || s.charAt(i) == ‘U’) {
System.out.println(“Found vowel ” + s.charAt(i) + ” at position ” + i);
}

Vowels program in java

Vowel program in java are discussed as follows

We created many programs to check and count vowels and consonants from String, StringBuffer, and from files.

Java program to print vowels in a String is as below

Java program to print vowels in a String
Fig: Java program to print vowels in a String

Java program to print vowels in a String

Steps to develop the program

  1. Take a String
  2. Create a loop to iterate each character of the string
  3. Inside loop check characters for vowel, both for upper and lower case
  4. If the character is a vowel then print its position and character
Java program to print vowels in a String
Fig: Java program to print vowels in a String

In the above program, we created for loop and it runs up to string length.

charAt() is used to access characters at specified index position.

The above program is simple we can make some improvements to it.

We are comparing both uppercase and lower case, we can convert complete string in lower case and compare it will reduce all uppercase comparison.

Q Using regular expressions to find vowels in a string in java

Q Using a switch statement to find vowels in a string in Java

How to find vowels in a string in java

write a java program to display all the vowels from a given string.

Output

count vowels in a string java Read from File

test.txt file are stored in d drive and file content is as below.

The Scanner class is used to read the test.txt file.

Vowels program in java using switch case

Read More: Switch Case in Java

Q: write a program to determine whether the input character is a vowel or consonant or not an alphabet

program to count the number of vowels and consonants in a given string in java

Character class contains method isAlphabetic.

This method is checking whether a number is an alphabet or not.

If the number is alphabet then match the character using for each loop with vowel characters stored in the array.

If matched then the character is vowel else consonant.

Java program to replace vowels in a string

How do you print the number of vowels and consonants in a string?

Read More

  1. Duplicate Number between 1 to n numbers
  2. Sum of two numbers using command line arguments
  3. Program to reverse a string
  4. Prime number program in java using while loop
  5. Print vowels in a String
  6. Square Star Pattern
  7. Registration Form in Java | Student registration form using java swing source code

Best Books for Java Beginner to Pro

Ebalagurusamy
Head First
Java Complete Reference
Java For Dummy
Effective Java
Let us Java