title: How do I count vowels in a string using recursion
body: <p>I'm trying to count the number of vowels in a string using recursion. Here is what I have so far:</p>
  
  <pre><code>int vowels(string str, int pos, int length)
  {
      if (str.length() == 0)
      return 0;
  
      switch (str[pos])
      {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
          return 1 + vowels(str.substr(1), pos, length);
        default:
          return vowels(str.substr(1), pos, length);
      }
  }
  
  int main()
  {
      string str;
      int len;
  
      cout &lt;&lt; "Enter a string: ";
      getline(cin, str);
      cout &lt;&lt; endl;
  
      len = static_cast&lt;int&gt;(str.length());
  
      cout &lt;&lt; "Number of vowels in \"" &lt;&lt; str &lt;&lt; "\" = "
           &lt;&lt; vowels(str, 0, len) &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>The problem is, I need to set pos to 0 only on the first time the vowels function is called without resetting it to 0 on subsequent recursive calls. Also, instead of using substrings, I need to increment pos before each recursive call of vowels(). Also, the base case should be when pos == length (when there are no more characters to check in the string).</p>