0

I'm learning how the functions work with c, and I was doing this exercice that must count the number of vocals in a string. The problem is that always outputs 4199835 vocals.

Here it is the code (Working on windows 10, and compiler devc++):

#include <stdio.h>
#include <string.h>
char cadena[100];
int vocals();
int voc,i;
int main(){
    fgets(cadena, sizeof(cadena),stdin);
    strtok(cadena,"\n");
    vocals(cadena);
    printf("Hi ha %d vocals",vocals);
}
int vocals(char string[100]){
    for (i=0;i<=strlen(string);i++){
        switch(string[i]){
        case 'a':
            voc++;
            break;
        case 'e':
            voc++;
            break;
        case 'i':
            voc++;
            break;
        case 'o':
            voc++;
            break;
        case 'u':
            voc++;
            break;
        }
    }
    return voc;
}
  • You need to initialize the variable to 0. – bereal Nov 25 '20 at 16:39
  • 4
    In `printf("Hi ha %d vocals",vocals)` the `vocals` isn't a variable: it is the function. Please look at compiler warnings. Try `printf("Hi ha %d vocals", vocals(cadena));` – Weather Vane Nov 25 '20 at 16:41
  • @WeatherVane there are no warnings. –  Nov 25 '20 at 16:43
  • 3
    Please enable warnings: *warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'int (__cdecl *)()'* – Weather Vane Nov 25 '20 at 16:44
  • 2
    @newbie_on_c In that case, given you are a (self-proclaimed) newbie, look into ways of increasing the warning level within the compiler _and take notice of them_. – TripeHound Nov 25 '20 at 16:45
  • `printf("Hi ha %d vocals",vocals)`-> `printf("Hi ha %s vocals",vocals(cadena));` `i <= strlen(string)` -> `i < strlen(string)`. `int vocals();` -> `int vocals(char string[]);`. And remove the `strtok` alltogether, it's pointless. Also `voc` as global variable is very poor design. There are probably more problems. – Jabberwocky Nov 25 '20 at 16:45
  • @WeatherVane thanks, now I see the error. Also I wasn't saving the output in any variable. I'm dumb. –  Nov 25 '20 at 16:47
  • @TripeHound I'll try it, thanks. –  Nov 25 '20 at 16:48
  • 2
    @WeatherVane you're right of course, comment edited – Jabberwocky Nov 25 '20 at 16:48
  • @Jabberwocky `strtok()` isn't _needed_ in this case (because having a trailing newline won't break the vowel-count), but having seen many questions on SO where failing to remove the newline (or not realizing it needed to be removed) broke things, it does no harm. – TripeHound Nov 25 '20 at 16:49
  • 2
    @TripeHound yes, you're right, it doesn't harm, but for removing the `\n` this is better: https://stackoverflow.com/a/28462221/898348 – Jabberwocky Nov 25 '20 at 16:50
  • There shouldn't be a need to remove the newline, because there's no reason to be storing this string at all! This problem is not line oriented, and `fgets` should be replaced with `fgetc` (although that would defeat the stated goal of using a function, I guess). – William Pursell Nov 25 '20 at 16:55
  • @WilliamPursell I wasn't saving the function output in any variable, I'm very dumb -_- –  Nov 25 '20 at 16:56
  • 1
    I thought there would be an `isvowel()` library function, but there isn't. As a (self-proclaimed) newbie you might like to know there is a family of [character classification functions](https://learn.microsoft.com/en-us/cpp/c-runtime-library/character-classification?view=msvc-160). – Weather Vane Nov 25 '20 at 17:01
  • 2
    @newbie_on_c You're not being dumb... in the case of `strtok`, you don't need to save the function's output because it modifies the string passed to it. Jabberwocky and William are also right that _in this case_ you don't actually _need_ to remove the trailing newline (since it doesn't affect the count), but being aware that you may need to in other situations is good. I'd also agree the method Jabberwocky linked is better (one I'd not seen before). – TripeHound Nov 25 '20 at 17:01

1 Answers1

1

A bit more universal:

size_t count(const char *str, const char *tosearch)
{
    size_t cnt = 0;
    while(*str)
    {
        if(strchr(tosearch, *str++)) cnt++;
    }
    return cnt;
}

usage

printf("%zu\n", count("Test sentence", "aeiouAEIOU"));
0___________
  • 60,014
  • 4
  • 34
  • 74