.Vowel Counting Function Difficulties in C

时间:2017-04-10 02:22:09

标签: c

Trying to build a vowel counting function for test practice but I am new to coding and can't figure out how to get the a char pointer to work correctly. What am I doing wrong?

int vowelCount(int index, char phrase[])
{

    int count = 0;
    if(phrase[index] == '\0')
    {
        return 0;
    }
    if(phrase[index] == 'a' || phrase[index] == 'e' || phrase[index] == 'i' || phrase[index] == 'o' || phrase[index] == 'u')
    {
        count = count + 1;
    }
    if(phrase[index] == 'A' || phrase[index] == 'E' || phrase[index] == 'I' || phrase[index] == 'O' || phrase[index] == 'U')
    {
        count = count + 1;
    }

    vowelCount(index + 1, phrase);
    return count;
}


int main (void)
{

    char array[1000];
    int index = 0;
    char inputPhrase;
    printf("Please enter a phrase: ");
    scanf("%c",&inputPhrase);

    while(inputPhrase != '\n')
    {
        array[index] = inputPhrase;
        index = index + 1;
        scanf("%c",&inputPhrase);
    }
    array[index] = '\0';
    index = 0;
    while(array[index] != '\0')
    {
        printf("%c",array[index]);
        index = index + 1;
    }
    index = 0;
    int numberOFvowels = vowelCount(index,array);

    printf("\n\nThere are %i vowels in the phrase.",numberOFvowels);
}

2 个答案:

答案 0 :(得分:2)

In your counting function you are basically abandoning the recursive result, just add it to the result:

int vowelCount(int index, char phrase[]) {
    int count = 0;
    if(phrase[index] == '\0')
    {
        return 0;
    }
    if(phrase[index] == 'a' || phrase[index] == 'e' || phrase[index] == 'i' || phrase[index] == 'o' || phrase[index] == 'u')
    {
        count = count + 1;
    }
    if(phrase[index] == 'A' || phrase[index] == 'E' || phrase[index] == 'I' || phrase[index] == 'O' || phrase[index] == 'U')
    {
        count = count + 1;
    }

    return count + vowelCount(index + 1, phrase); // add the result together here
}

However, unless you are trying to practice recursion, there is no point to do this counting in recursive, just make a table of vowels and loop through the string is much easy to write and easy to understand:

static int table[256] = {
    ['a']=1,['e']=1,['i']=1,['o']=1,['u']=1,
    ['A']=1,['E']=1,['I']=1,['O']=1,['U']=1
};
int vowelCount(const char *phrase) {
    int count = 0;
    while (*phrase) {
        if (table[*phrase] == 1) {
            count += 1;
        }
        phrase++;
    }
    return count;
}

答案 1 :(得分:0)

You need to pass your count in your recurssion.

#include<stdio.h>

int vowelCount(int index, char phrase[],int count) {

//int count = 0;
if(phrase[index] == '\0')
{
    return count;
}
if(phrase[index] == 'a' || phrase[index] == 'e' || phrase[index] == 'i' || phrase[index] == 'o' || phrase[index] == 'u')
{
    count = count + 1;
}
if(phrase[index] == 'A' || phrase[index] == 'E' || phrase[index] == 'I' || phrase[index] == 'O' || phrase[index] == 'U')
{
    count = count + 1;
}

count=vowelCount(index + 1, phrase,count);
return count;
}

char main () {

char array[1000];
int index = 0;
char inputPhrase;
printf("Please enter a phrase: ");
scanf("%c",&inputPhrase);

while(inputPhrase != '\n')
{
    array[index] = inputPhrase;
    index = index + 1;
    scanf("%c",&inputPhrase);
}
array[index] = '\0';
index = 0;
while(array[index] != '\0')
{
    printf("%c",array[index]);
    index = index + 1;
}
index = 0;
int numberOFvowels = vowelCount(index,array,0);

printf("\n\nThere are %i vowels in the phrase.",numberOFvowels);
}