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

标签 c arrays

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

#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/

相关文章:

c - 如何调用使用const和*的过程?

C 中字符串中特定字母的大写

c - 通过 cURL(或 FTP)将值传递给 C 二进制文件

python - numpy 数组的元素明智测试是数字

c - 在 C 中通过 SSH 将文件/目录从本地计算机传输到远程计算机?

c - 使用 GNU/GCC 四精度库出错

PHP寻找一个从字符串中获取值的函数

java - 反转字符串数组中的字符

c# - 我如何移动列表末尾的列表对象,同时将其他对象移回?

arrays - Googlefinance - 返回高价和日期