C函数删除字符串中的元音不起作用? [英] C function to remove vowels in a string not working?
本文介绍了C函数删除字符串中的元音不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我删除字符串中元音的功能;
Here is my function to remove the vowels in a string;
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;
}
问题在于它正在返回输入的原始字符串.有什么想法吗?
The problem is that it is returning the original string inputted. Any ideas?
推荐答案
||
和&&
运算符之间存在混淆.您要测试字符是否与'a'
AND 和'e'
等不同
There is a confusion between the ||
and &&
operators. You want to test that the character is different from 'a'
AND different from 'e'
etc.
这是修改后的版本:
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
是否应被视为元音仍有待确定.
Note however that uppercase vowels are not removed by this function, and whether y
should be considered a vowel remains to be decided.
这篇关于C函数删除字符串中的元音不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文