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

  • 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;
}
}

--

--

Software Engineer, who loves challenges and new technologies. :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
sandeep negi

Software Engineer, who loves challenges and new technologies. :)