CodeLab Solution 20858




CS125 CodeLab Solutions

CodeLab Solution 20858

Write a recursive, boolean -valued method , containsVowel, that accepts a string and returns true if the string contains a vowel.

A string contains a vowel if:

The first character of the string is a vowel, or
The rest of the string (beyond the first character ) contains a vowel

[cc lang=”JAVA”] public boolean containsVowel(String sentence){ if (sentence.isEmpty()) return false; if (sentence.charAt(0) == ‘a’ || sentence.charAt(0) == ‘e’ || sentence.charAt(0) == ‘i’ || sentence.charAt(0) == ‘o’
|| sentence.charAt(0) == ‘u’ || sentence.charAt(0) == ‘A’ || sentence.charAt(0) == ‘E’ || sentence.charAt(0) == ‘I’
|| sentence.charAt(0) == ‘O’ || sentence.charAt(0) == ‘U’) return true; else return containsVowel(sentence.substring(1,sentence.length()));}
[/cc]


No Comments Yet.

Leave a comment

You must be Logged in to post a comment.