We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,145 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

recursions

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

This is one of the recursion exercises I couldn't get right.
Any suggestions? Thanks!

My code:

    bool containsVowel (string s) {
        if (s.substr(0,1)=="a") return true;
        if (s.substr(0,1)=="e") return true;
        if (s.substr(0,1)=="u") return true;
        if (s.substr(0,1)=="o") return true;
        if (s.substr(0,1)=="i") return true;
        containsVowel (s.substr(1,s.length()-1));
    }
7
Contributors
9
Replies
2 Weeks
Discussion Span
4 Months Ago
Last Updated
11
Views
Question
Answered
marnun
Light Poster
28 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

For starters, I would turn your testing into a series of if..else if statements, including the final recusive call. Then you need and else case that returns false.

vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6

hi, thanks for answering.
still get error from codeLab

    bool containsVowel (string s) {
        if (s.substr(0,1)=="a") return true;
        else if (s.substr(0,1)=="e") return true;
        else if (s.substr(0,1)=="u") return true;
        else if (s.substr(0,1)=="o") return true;
        else if (s.substr(0,1)=="i") return true;
        else 
            containsVowel (s.substr(1,s.length()-1));
        return false;
    }

this is the error message:

Remarks:

     ⇒     terminate called after throwing an instance of
               'std::out_of_range'
     ⇒     what(): basic_string::substr

     ⇒     Your code had an error during execution

marnun
Light Poster
28 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Still missing parts.

Base case - when string being examined is empty.

The recusive call needs to be in an if..else, and it's return needs to be returned.

The return false needs to be in an else block.

vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6

You are getting the out of bounds from line 8. Before you try to length() - 1, do a check to see if length == 0. If true, return false.

T-Dogg3030
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0
bool containsVowel (string s) 
{
    if (0 == s.length())
        return false;

    if ( (s.substr(0,1)=="a") || (s.substr(0,1)=="e") || (s.substr(0,1)=="i") ||
        (s.substr(0,1)=="o") || (s.substr(0,1)=="u") ) 
        return true;

    return containsVowel (s.substr(1, s.length() - 1));
}

I'll leave you to work out handling uppercase vowels.

nullptr
Junior Poster
154 posts since Mar 2012
Reputation Points: 46
Solved Threads: 32
Skill Endorsements: 0

The thinking mainly is wrong, because, on recursive calls, if indeed a vowel is found, it will still return false, because of the last condition. I suggest you take an extra variable, if allowed, which will carry the information:

void containsVowel (string s, bool &isIt) {
    if (s.empty()) return;
    if (s[0]=='a' || s[0]=='e' || s[0]=='u' || s[0]=='o' || s[0]=='i') isIt=true;
    containsVowel(s.substr(1, s.size()-1), isIt);
}

int main(){
    string a="bcdghijklmnpqrst";
    bool isIt=false;
    containsVowel(a, isIt);
    if (isIt) cout<<"True.\n";
    else cout<<"Not true.\n";
    return 0;
}

Let me explain a bit what I mean, regarding your function:

string a="bcdfghijklmnpqrst"

this string conains the vowel i.

when calling your function, eventually will return true from finding the vowel:
here are the recursive returns:


"t" return false
"st" return false
"rst" return false
"qrst" return false
"pqrst" return false
"npqrst" return false
"mnpqrst" return false
"lmnpqrst" return false
"klmnpqrst" return false
"jklmnpqrst" return false
"ijklmnpqrst" return true // we found a solution
"hijklmnpqrst" return false //here the return changes, because 'h' is not a vowel.
"ghijklmnpqrst" return false
"fghijklmnpqrst" return false
"dfghijklmnpqrst" return false
"cdfghijklmnpqrst" return false
"bcdfghijklmnpqrst" return false

So, even thou your word conains vowels, it will still return false.
Again, I don't know if your teacher allowes you to make it into a void, and take an extra value which will hold the information.

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

There is no need for additional variables.

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

bool isVowel(const char symbol)
{
    return (string("aeiou").find(tolower(symbol)) != string::npos);
}

bool containsVowel(const string text)
{
    return ((text.length() > 0) && (isVowel(text[0]) || containsVowel(text.substr(1))));
}

int main()
{
    // Stuff with containsVowel..
    return 0;
}
Gonbe
Posting Whiz in Training
231 posts since Jan 2010
Reputation Points: 33
Solved Threads: 29
Skill Endorsements: 9
public boolean containsVowel ( String a)
{
if  (a.length() == 0)
    return false;

if ((a.charAt(a.length()-1)==('a'))||
      ((a.charAt(a.length()-1)==('e'))||
        ((a.charAt(a.length()-1)==('i'))||
            ((a.charAt(a.length()-1)==('o'))||
                ((a.charAt(a.length()-1)==('u')))))))
                    return true;
if ((a.charAt(a.length()-1)==('A'))||
      ((a.charAt(a.length()-1)==('E'))||
        ((a.charAt(a.length()-1)==('I'))||
            ((a.charAt(a.length()-1)==('O'))||
                ((a.charAt(a.length()-1)==('U')))))))
                    return true;
else 
 return containsVowel(a.substring(0, a.length()-1));
}
YaLeon
Newbie Poster
10 posts since Dec 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

Thanks everybody for extensive explanations - been very very helpful.
i ended up submitting the following code:

    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;
    }
marnun
Light Poster
28 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Months Ago by vmanes, T-Dogg3030, Gonbe and 3 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0858 seconds using 2.7MB