【发布时间】:2011-09-05 09:44:29
【问题描述】:
我只是想使用递归计算字符串中的元音,但它不起作用。
#include <stdio.h>
#include <string.h>
#define SETSIZ 10
#define TRUE 1
#define FALSE 0
int is_empty(const char *set);
int is_element(char vowel, const char *set);
int is_vowel(const char *vowels, const char *set);
int main(void)
{
int count = 0,i;
char vowels[11] = {'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', '\0'}, set[SETSIZ] = "mustafa";
for(i=0;i<strlen(set);i++){
if(is_vowel(vowels, set))
count += 1;
}
printf("%s has %d vowels",set, count);
return(0);
}
int is_empty(const char *set)
{
return(set[0] == '\0');
}
int is_element(char vowel, const char *set)
{
int ans;
if(is_empty(set))
ans = FALSE;
else if (vowel == set[0])
ans = TRUE;
else
ans = is_element(vowel, &set[1]);
return(ans);
}
int is_vowel(const char *vowels, const char *set)
{
int ans, i = 0;
if(is_empty(vowels))
ans = FALSE;
else if(is_element(vowels[0], set))
{
printf("**");
ans = TRUE;
}
else
{
printf("--");
ans = is_vowel(&vowels[1], set);
}
return(ans);
}
【问题讨论】:
-
在
main中循环遍历set(在该范围内似乎不存在),并始终将相同的内容传递给is_vowel。你永远不会使用循环计数器i。 -
set在滚动区域中定义。我也认为它一开始没有定义。致 OP:@paranoidgnu -- 将行长限制为 80 个字符 :)