Write a recursive, bool-valued function, 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

Write a recursive, bool-valued function, 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

LANGUAGE: C++

CHALLENGE:

Write a recursive, bool-valued function, 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

SOLUTION:



bool containsVowel (string s) {
        bool hasVowel=false;
        if (s.length()==0) return false;
        else if (s[0]=='a'||
                    s[0]=='e'||
                    s[0]=='u'||
                    s[0]=='o'||
                    s[0]=='i'||
                    s[0]=='A'||
                    s[0]=='E'||
                    s[0]=='U'||
                    s[0]=='O'||
                    s[0]=='I')
            hasVowel=true;
        else
            hasVowel=containsVowel(s.substr(1,s.length()-1));
        return hasVowel;
    }

Add comment