How to print vowels in a string in java

You can check for the presence of vowels(a, e, i, o, u) in a string and list them using a java program. There are following two ways in which this can be done.

Method 1: Iteration over the string
Follow the below steps to write a program to check the vowels in a string using iteration.

  1. Initialize a string with all the vowels.
  2. Iterate over the characters of the string in which you want to check the vowels.
  3. In very iteration, check if the current character is from among the characters of string in Step 1(all vowels).
  4. If it is, then it is a vowel and it is printed else check with the next character.
public class VowelCheckerIteration {
   public static void main(String[] args) {
      // source string
      String str = "codippa";
      // all vowels
      String vowels = "aeiou";
      System.out.print("Vowels in the string are: ");
      // iterate over the characters in string
      for (char character : str.toCharArray()) {
         // check if current character is a vowel
         if (vowels.indexOf(character) != -1) {
            System.out.print(character + " ");
         }
      }
   }
}

Output is

Vowels in the string are: o i a 3

Note that we use indexOf method of java.lang.String class to check if it contains a character. This method will return -1 if the character does not exist in the string, otherwise its index(or position) in the string.
You can also use contains method of java.lang.String. This method accepts a string argument which will be checked to be present and returns true if the string is present, false otherwise.
To use contains, you need to convert character to a string using Character.toString method as shown below.

if (vowels.contains(Character.toString(character)) != -1) {
   System.out.print(character + " ");
}

Method 2: Using Regular expression
A regular expression can be used to determine the vowels in a string. The regular expression string which will find the occurrence of a vowel in a string is “[aeiou]”.
It will check if any of the characters between square brackets exists in the string. Example,

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class VowelCheckerRegularExpression {
   public static void main(String[] args) {
      // source string
      String str = "codippa";
      // compile regular expression
      Pattern compile = Pattern.compile("[aeiou]");
      // match for regular expression in string
      Matcher matcher = compile.matcher(str);
      // hold the result
      System.out.print("Vowels in the string are: ");
      // loop till the regular expression matches in the string
      while(matcher.find()) {
         // get the matched string
         String vowel = matcher.group();
         System.out.print(vowel +" ");
      }
      System.out.println(count);
   }
}

compile method of java.util.regex.Pattern class accepts a string argument and compiles it into a pattern. This pattern object is used to create a java.util.regex.Matcher object using its matches method.

Matcher object then searches for the presence of regular expression in the given string using its find method. find method returns true if it finds the pattern in the string. Multiple invocations of find method keep returning true till the end of the string is reached or any invocation returns false.
The pattern matched can be retrieved using the matcher’s group method.
Output of the above program is

Vowels in the string are: o i a

Click the clap below if the post was helpful.

sdasdasd

Leave a Reply