c - Recursive call that confuses me -
i have following function
int vowels(char *str) { int count = 0; if (! str[0]) return count; if (strchr("aeiouaeiou", str[0])) count ++; return count + vowels(&str[1]); } that performs counting vowels appear in string, confuses me recursive call vowels(&str[1]) not understand why on every call goes next character, without doing str++. can me understand this? please.
within each function call declared like
int vowels(char *str); though declare like
size_t vowels( const char *str ); expression
&str[1] is equivalent to
str + 1 or
++str however may not use
str++ because value of expression address stored in str before increment.
as me define function following way
size_t vowels( const char *s ) { return *s ? ( strchr( "aeiouaeiou", *s ) != null ) + vowels( s + 1 ) : 0; }
Comments
Post a Comment