2

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);
}
fluter
  • 13,238
  • 8
  • 62
  • 100
Nate
  • 21
  • 1
  • Why oh why are we reading character by character and then using a recursive counting funtion that never "uses" count? – John3136 Apr 10 '17 at 02:28
  • Using `char main()` is a new trick — see [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/) for details. – Jonathan Leffler Apr 10 '17 at 02:39

2 Answers2

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;
}
fluter
  • 13,238
  • 8
  • 62
  • 100
  • Just curious, if the maximum printable ASCII value is `126`, why `table[256]`? The solution is fine, just curious really. – David C. Rankin Apr 10 '17 at 07:43
  • You are right, this is just a defensive measure, so that the table can cover all possible values of a char data type. If the caller can make sure the input string is printable, yeah, the smaller table should be used. – fluter Apr 10 '17 at 07:53
  • That was exactly what was wrong! Thank you guys so much! – Nate Apr 10 '17 at 14:14
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);
}
Nirvedh Meshram
  • 439
  • 6
  • 13