编写一个递归函数来计算数组中的元音

时间:2015-01-06 14:04:52

标签: c function recursion

到目前为止,这是我的进展,我已经收到了这个错误"分配使得整数指针没有强制转换"如果有人纠正我的代码并告诉我我的缺陷在哪里,我将非常感激。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int vowels(char *str[],int len)
{
    int i,s=0;

    if (!str || len<1)
        return s;

    for(i=0;i<len;i++){
        if (str[i]='a') || (str[i]='e') || (str[i]='i') || (str[i]='o') || (str[i]='u')
        || (str[i]='A') || (str[i]='E') || (str[i]='I') || (str[i]='O') || (str[i]='U'){
            return s++;
        }
    }

    return s;
}


int main (void)
{
    int i,len;
    int pom;
    char *str[10];

    len=strlen(*str);

    for(i=0;i<len;i++){
        scanf("%s",str[i]);
        pom=vowels(str,len);
        printf("%d",pom);
    }

    return 0;
}

4 个答案:

答案 0 :(得分:2)

以防万一递归很重要(请注意签名的变化):

int vowels(char *str,int len)
{
    if (!str || len<1)
        return 0;
    if ((*str=='a') || (*str=='e') || (*str=='i') || (*str=='o') || (*str=='u') || (*str=='A') || (*str=='E') || (*str=='I') || (*str=='O') || (*str=='U'))
        return 1 + vowels( str+1, len-1 );
    else
        return vowels( str+1, len-1 );
 }

答案 1 :(得分:2)

更新:因为这个答案已被接受,但它没有完全回答这个问题(它没有包含函数vowels())我添加了我的递归版本函数vowels()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/**
 * Return the number of vowels in a string.
 *
 * The characters treated as vowels are:
 *      'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'.
 * The function checks the first character of the input string 
 * then calls itself recursively for the rest of the string.
 *
 * @param  string str    the string to check for vowels
 * @return int           the number of wovels found in the string
 */
int vowels(char *str)
{
    /* The number of vowels found in the string so far */
    int count = 0;

    /* Recursion stop condition: when the string is empty */
    if (! str[0]) {
        /* An empty string does not contain any vowels */
        return count;
    }

    /* Check the first character of the string */
    if (strchr("aeiouAEIOU", str[0])) {
        /* Found a vowel, count it */
        count ++;
    }

    /* Recursive call to find the number of vowels after the first character */
    return count + vowels(&str[1]);
}


int main(void)
{
    int i, len;
    int pom;
    char *str[10];

    int count = sizeof(str)/sizeof(str[0]);

    for (i = 0; i < count; i ++) {
        /* Hopefully you will not input strings longer than 254 characters */
        str[i] = (char *)malloc(255);

        fgets(str[i], 255, stdin);

        pom = vowels(str[i]);
        printf("%d\n", pom);

        /* Release the memory */
        free(str[i]);
    }

    return 0;
}

答案 2 :(得分:1)

if (str[i]='a')

应该是

if (str[i]=='a')

依旧......

还:

return s++;

应该是

s++;

答案 3 :(得分:1)

#include <stdio.h>
#include <ctype.h>

int vowels(char *str, int count){
    if (!*str)
        return count;

    char ch = tolower(*str);
    int match = ch=='e' || ch=='a' || ch=='i' || ch=='o' || ch=='u';
    return vowels(++str, count + match);
}


int main (void){
    int i, t, pom;
    char str[32];

    scanf("%d", &t);

    for(i=0;i<t;i++){
        scanf("%31s",str);
        pom=vowels(str, 0);
        printf("%d\n",pom);
    }

    return 0;
}

如何编写递归函数

Type function_name ( Parameter declarations ) {
    if( Termination condition of recursion ) {
        return [ End value ]; 
    }
    else {
        Calculation for the next call;
        //Tail recursion call optimization can be expected by the simple call to the required variables to parameters.
        return function_name ( Parameters );
    }
}