0

Im trying to write a function that detects vowels and digits in a string. iterating through the string, im trying to do a one-line if statement to check if a character is a vowel. Code as below...

void checkString(char *str)
{
    char myVowels[] = "AEIOUaeiou";

    while(*str != '\0')
    {
        if(isdigit(*str))
            printf("Digit here");
        if(strchr(myVowels,*str))
            printf("vowel here");
        str++;
    }
}

The digit checking works perfectly. However "(strchr(myVowels,*str))" doesnt work. It says different types for formal and actual parameter 1. Can anyone help me here? Thanks

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
kype
  • 555
  • 1
  • 6
  • 24

1 Answers1

1

Most likely you haven't included proper header files.

This works just fine:

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

void checkString(const char *str)
{
    char myVowels[] = "AEIOUaeiou";

    printf("checking %s... ", str);

    while(*str != '\0')
    {
        if(isdigit(*str))
            printf("Digit here ");
        if(strchr(myVowels,*str))
            printf("vowel here ");
        str++;
    }

    printf("\n");
}

int main(void)
{
  checkString("");
  checkString("bcd");
  checkString("123");
  checkString("by");
  checkString("aye");
  checkString("H2CO3");
  return 0;
}

Output (ideone):

checking ... 
checking bcd... 
checking 123... Digit here Digit here Digit here 
checking by... 
checking aye... vowel here vowel here 
checking H2CO3... Digit here vowel here Digit here 
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Note that it's often necessary to convert the argument to the ``'s `is...()` functions to `unsigned char`. See [the manual page](http://www.manpagez.com/man/3/ctype/). – unwind Feb 11 '13 at 10:59
  • @unwind Interesting detail, thanks. I'd assume we're dealing with ASCII chars (as EBCDIC and others are virtually extinct) and so all is well. – Alexey Frunze Feb 11 '13 at 11:05
  • Guess it was something to do with missing headers. Thanks works perfectly now – kype Feb 11 '13 at 11:23
  • @user1234897 To prevent such bugs in the future, get a modern C compiler with support for a newer version of the C standard (C99 or later). If you had forgotten to include that header on a C standard compiler, you would have gotten a compiler error. – Lundin Feb 11 '13 at 12:15