scanf 格式化输入不适用于扫描的第一个字符

scanf formatted input does not apply to first character scanned

我正在尝试编写一个输出非元音字符的程序(没有 if 语句并使用格式化的 scanf 输入)。我目前拥有的代码不会将 %*[] 忽略的字符应用于扫描的第一个 %c 字符,但该限制适用于其他字符。例如,“Andrew”变成“Andrw”而不是“ndrw”。我怀疑这可能是由于开头的 %c 造成的。有人可以帮我吗? :)

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

int main(void) {
    char c;
    while (scanf("%c%*[aeiouAEIOU]", &c) == 1)
        printf("%c", c);
    return 0;
}

scanf 格式在 order 中匹配,因此 %cfirst 中匹配 A。您需要为此使用 2 separate scanfs,或者在循环之前使用 initial-vowel eating scanf:

scanf("%*[aeiouAEIOU]");
while (scanf("%c%*[aeiouAEIOU]", &c) == 1) { 
    printf("%c", c);
}

问题是这是否比

更清晰更好
int c;
while ((c = getchar()) != EOF) {
    if (! strchr("aeiouAEIOU", c)) {
        putchar(c);
    }
}

我有一个自以为是的答案...