-int vowelcount (char [],int,int)
{
}
i just cant come out with one a recursive way to a fuction to count vowels in a given string
even if u just can give me clues would be great
thanksanother answerer had this almost right.
you need to use sngle quotes since you are comparing single characters (not strings)
int vowelcount (char *str){
return *s?(*str == 'a'||*str == 'o' || *str == 'u'|| *str == 'i'|| *str == 'e')+ vowelcount(s+1):0;
}
What is happening here is:
*s? // check for end of string *s *s has value 0 if s points to the end of the string
if not zero we evaluate the following expression (if false (0) we evaluate the expression following the : )
(*str== 'a' || .... check if the character pointed to is a vowel
in a boolean statement true is 1 and false is 0
so we add that value to the return value of calling vowelcount on the next character in the string
(s+1 points to the next character )
: 0 we return 0 if we are at the end of the string i.e. *s is 0 (false)
Go though each character of your string and use IndexOf to see if it part of the string "aeiou"
If the answer is greater than -1 then it is, so increment the vowel counter
Move on to the next character and repeat.
For a simple example see http://msdn.microsoft.com/en-us/library/鈥?/a> - Examples given are VB, C++ and C#
Recursion:
int vowelcount (char *str){
return ((*str == "a" | |*str == "o" || *str == "u" || *str == "i" || *str == "e")? 1 : 0) + (*str == 0)? 0 : vowelcount(++str);
}
It may not be 100% correct.
Iteration:
int vowelcount (char *str){
int count = 0;
do
{
if (*str == "a" | |*str == "o" || *str == "u" || *str == "i" || *str == "e")
++count;
}while (*++str != 0);
return count;
}
没有评论:
发表评论