Write a recursive function, vowels, that returns the number of vowels in a string (character array).no global variables are allowed ??

Recommended Answers

All 5 Replies

And your code?

int Vowels(const string s}
i should check if string is empty return 0
then i must check the characters in string ..
ill remove 1 charcter and check if its a vowel so counter ++
then call the function with the string shortend by the one i removed and recheck .. thats my idea but im lost and i cant translate it into the code i want .. errrrrors :/

Here's a big hint which counts the characters.

#include <iostream>

unsigned long vowels(char ca[], unsigned long v_num)
{
    if ( ca[0] == '\0' )
        return v_num;

    vowels(++ca, ++v_num);
}

int main(int argc, char** argv)
{
    char str[] = "This is a string to check for vowels";

    std::cout << "'" << str << "'" << " has " << vowels(str, 0) << " characters" << std::endl;

    return 0;
}

`

i tried this one but i cant solve errors .. help please :)



#include <iostream>
#include <string>
using namespace std;


    int vowles(string  x)
`Inline Code Example Here`
    char str;
          int counter=0;
        if (x == 0)
        return counter;
        if(str[x-1] == 'a' 
            || str[x-1] == 'e'
            || str[x-1] == 'i' 
            || str[x-1] == 'o' 
            || str[x-1] == 'u' 
            || str[x-1] == 'A' 
            || str[x-1] == 'E' 
            || str[x-1] == 'I' 
            || str[x-1] == 'O' 
            || str[x-1] == 'U')

        {   counter++;

        counter = vowles(x-1);
        return counter;
        }





void  main ()
     {

      char str[80];
    int vowel;
    string x;

    cout << "plese insert your sentence you want to check :) "<<endl;
    cin.getline(str,80);

    x = str[80]; 
    vowel = vowles(x);

    cout << endl <<  "the number of vowles is :    " <<  vowel << endl;
}

`

Last hint.

#include <iostream>

unsigned long vowels(char ca[])
{
    if ( ca[0] == '\0' )
        return 0;
    //decide if ca[0] is a vowel
    return 1 + vowels(++ca);
}

int main(int argc, char** argv)
{
    char str[] = "This is a string to check for vowels";
    std::cout << "'" << str << "'" << " has " << vowels(str) << " characters" << std::endl;
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.