Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 检测字符串中的元音_C_String_Strchr - Fatal编程技术网

C 检测字符串中的元音

C 检测字符串中的元音,c,string,strchr,C,String,Strchr,我正在尝试编写一个函数来检测字符串中的元音和数字。在字符串中迭代,我尝试执行一行if语句来检查字符是否为元音。代码如下 void checkString(char *str) { char myVowels[] = "AEIOUaeiou"; while(*str != '\0') { if(isdigit(*str)) printf("Digit here"); if(strchr(myVowels,*str))

我正在尝试编写一个函数来检测字符串中的元音和数字。在字符串中迭代,我尝试执行一行if语句来检查字符是否为元音。代码如下

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

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

数字检查工作得很好。但是“(strchr(my元音,*str))不起作用。它表示形式参数1和实际参数1的不同类型。有人能帮我吗?谢谢

很可能您没有包含正确的头文件

这很好:

#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;
}

是否包含
string.h
?在将str*传递给function@Techmonk您不需要强制转换任何内容,因为
*str
已经是
char
。它应该按原样工作。只有两个音符:(1)BTW,英语不认为“Y”也是元音吗?(我是捷克人,所以这里不确定。但在捷克语中也是元音。)(2)注意一些语言有更多元音(例如,ě,á,í,é等。)@mity
y
是“模糊的”。在像
type
patient
这样的词中是元音,但在像
boy
yell
这样的词中不是元音。请注意,通常需要将参数转换为
is…()
函数转换为
无符号字符。请参阅。@解开有趣的细节,谢谢。我假设我们正在处理ASCII字符(因为EBCDIC和其他字符实际上已经灭绝),所以一切都很好。我猜这与缺少的头有关。谢谢你真是太棒了now@user1234897为了防止将来出现此类错误,请使用支持更新版本的C标准(C99或更高版本)的现代C编译器。如果您忘记在C标准编译器中包含该头,那么您将得到一个编译器错误。
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