Skip to main content

Command Palette

Search for a command to run...

Java Coding Series: How to check vowel is present in the string?

Updated
1 min read
Java Coding Series: How to check vowel is present in the string?
S

I love learning about technology and sharing that with others

  • Approach 1 : Most Useful, We will use regex to find wether vowels present in the stirng or not.

public static Boolean isVowelPresent(String sample) {

    return sample.toLowerCase().matches(".\*\[aeiou\].\*");  
}
  • Approach 2 : Traversing one by one and finding whether vowel is present or not using indexOf method

public static Boolean isVowelPresent(String sample) {
char vowelArr[] = { 'a', 'e', 'i', 'o', 'u' };

    for (int i = 0; i < vowelArr.length; i++) {  
        char currentVowel = vowelArr\[i\];

       if(sample.indexOf(currentVowel)!=-1){  
           return true;  
       }  
    }  
    return false;  
}  

}

  • Approach 3 : Traversing one by one and finding the vowel old fashion

public static Boolean isVowelPresent(String sample) {
char vowelArr[] = { 'a', 'e', 'i', 'o', 'u' };

    for (int i = 0; i < vowelArr.length; i++) {  
        char currentVowel = vowelArr\[i\];

        for (int j = 0; j < sample.length(); j++) {  
            if (currentVowel == sample.charAt(j)) {  
                return true;  
            }  
        }  
    }  
    return false;  
}  

}

74 views

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)