Write a method called isAllVowels that returns whether a string consists entirely of vowels (a, e, i, o, or u, case- insensitively). If and only if every character of the string is a vowel, your method should return true. For example, the call isAllVowels("eIEiO") returns true and isAllVowels("oink") returns false. You should return true if passed the empty string, since it does not contain any non-vowel characters.
Method definition of “ isAllVowels ”:
// Method definition of “isAllVowels”
public static boolean isAllVowels(String string)
{
// Check if the string is empty and return true
if (string.equals(""))
{
return true;
}
// Check each character in the String
// whether its vowel or not. Also if the string is
// empty control will directly go to the return
// statement after for loop
for (int i = 0; i < string.length(); i++)
{
// Get individual character in char
char c = string.charAt(i);
// If character is a vowel proceed and check next
//character else return false
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' || c == 'A' ||
c == 'E' || c == 'I' || c == 'O' || c == 'U')
{
continue;
}
else
{
return false;
}
}
// If no consonant is found then only the
// program control will reach here and true
// will be return (all characters are vowels)
return true;
}
Complete Program Code:
NOTE: The following executable program code is used to call the above method “isAllVowels” in multiple times and print the final results as on screen.
//Create a main class name, "VowelsDemo"
public class VowelsDemo
{
// Method definition of “isAllVowels”
public static boolean isAllVowels(String string)
{
// Check if the string is empty and return true
if (string.equals(""))
{
return true;
}
// Check each character in the String
// whether its vowel or not. Also if string is
// empty control will directly go to the return
// statement after for loop
for (int i = 0; i < string.length(); i++)
{
// Get individual character in char
char c = string.charAt(i);
// If character is a vowel proceed and check // next character else return false
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' || c == 'A' ||
c == 'E' || c == 'I' || c == 'O' ||
c == 'U')
{
continue;
}
else
{
return false;
}
}
// If no consonant is found then only the
// program control will reach here and true
// will be return (all characters are vowels)
return true;
}
// main method
public static void main(String[] args)
{
//Call the method isAllVowels with string "eIEio"
//and display the result
System.out.println("isAllVowels(\"eIEio\") "
+ " returns "+isAllVowels("eIEio"));
//and display the result
System.out.println("isAllVowels(\"oink\")"
+ "returns "+isAllVowels("oink"));
//Call the method isAllVowels with empty string
//and print the result
System.out.println("isAllVowels(\"\")"
+ " returns "+isAllVowels(""));
}
}
Sample Output: