Recursive program to find vowels
Jan 29, 2012 at 8:34pm UTC
This program uses recursion to find the number vowels in a string. Can someone please explain to me what the purpose of "subscript" is in the program. The program compiles and runs perfectly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <string>
using namespace std;
int vowels( string, int );
bool isVowel(char );
int main()
{
string array;
cout<<"Enter a string: " ;
getline(cin,array);
cout << "The are " << vowels( array, 0) << " vowels in \"" <<array<<"\"\n" ;
system("pause" );
return 0;
}
int vowels( string str, int subscript)
{
if (str[subscript] == '\0' )
{
return 0;
}
else
if (isVowel(str[subscript]) )
{
return 1+vowels( str,subscript+1);
}
else
{
return vowels( str, subscript+1);
}
}
bool isVowel(char in)
{
switch (in)
{ case 'a' :
case 'A' :
case 'e' :
case 'E' :
case 'i' :
case 'I' :
case 'o' :
case 'O' :
case 'u' :
case 'U' : return true ;
}
return false ;
}
Jan 29, 2012 at 9:02pm UTC
subscript
defines which letter in the string is being examined.
Jan 29, 2012 at 9:13pm UTC
thank you.
Topic archived. No new replies allowed.