gpt4 book ai didi

c - 如何仅在C中接受字符输入?

转载 作者:行者123 更新时间:2023-11-30 20:54:12 24 4
gpt4 key购买 nike

该程序仅计算您在程序中输入的单词中的元音。但我希望这个程序只接受字符而不是数字。有办法吗?

#include <stdio.h>
#define MAX 50

int countVowel(char[]);

void main()
{
char text[MAX];
int cVowel, sum;
printf("Enter text : ");
scanf("%s", text);
cVowel = countVowel(text);
printf("Text : [%s] has %d vowels", text, cVowel);

}

int countVowel(char t[])
{
int i = 0, count = 0;
while (i < MAX && t[i] != '\0')
{
if (t[i] == 'A' || t[i] == 'a' || t[i] == 'E' || t[i] == 'e'
|| t[i] == 'I' || t[i] == 'i' || t[i] == 'O' || t[i] == 'o'
|| t[i] == 'u' || t[i] == 'U')

count++;
i++;

}
return (count);

}

我尝试过使用 atoi 但没有成功:/

最佳答案

您可以使用strpbrk:

Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.

int main(void) /* void main is not a valid signature */
{
char text[MAX];
int cVowel, sum;

while (1) {
printf("Enter text : ");
scanf("%s", text);
if (strpbrk(text, "0123456789") == NULL) {
break;
}
}
...
}

关于c - 如何仅在C中接受字符输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42852960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com