这是我删除字符串中元音的功能;

char *removeVowels(char *inString) {

    int count = 0; //to count the non vowel characters

    for (int i = 0; inString[i]; i++)
        if (inString[i] != 'a' || inString[i] != 'e' || inString[i] != 'u' || inString[i] != 'o' || inString[i] != 'i')
            inString[count++] = inString[i]; //if character is not a vowel placed at count++

    inString[count] = '\0';

    return inString;
}


问题在于它正在返回输入的原始字符串。有任何想法吗?

最佳答案

||&&运算符之间存在混淆。您要测试字符是否不同于'a'并且不同于'e'等。

这是修改后的版本:

char *removeVowels(char *inString) {
    int count = 0; // index for preserved characters

    for (int i = 0; inString[i]; i++) {
        if (inString[i] != 'a' && inString[i] != 'e' && inString[i] != 'i'
        &&  inString[i] != 'o' && inString[i] != 'u') {
            inString[count++] = inString[i]; // copy the non-vowel character
        }
    }
    inString[count] = '\0';  // set the null terminator.

    return inString;
}


但是请注意,此功能不会删除大写元音,是否应将y视为元音仍有待确定。

09-25 20:57