我可以使用大量OR更简洁的if语句吗?

shm*_*ink 1 c if-statement

我通过做一些随机练习来提升我对C的认识.以下是我计算字符串中元音数量的解决方案的摘录.它有效,但必须有一个更简洁的方式来编写这个if语句.有任何想法吗?

for(int i = 0; i < length; i++) {
    if(input[i] == 'a' || input[i] == 'e' || input[i] == 'i' || input[i] == 'o' || input[i] == 'u') {
        total++;
    }
}
Run Code Online (Sandbox Code Playgroud)

dbu*_*ush 6

您可以通过以下switch案例执行此操作:

for(int i = 0; i < length; i++) {
    switch (input[i]) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        total++;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)