Tam*_*ici 3 c arrays string logical-operators
我试图通过将所有辅音放入不同的数组然后将第一个数组重新初始化为第二个数组来删除字符串的所有元音.之后,应该打印第一个数组,即只有辅音.我真的不确定问题出在哪里,我一直在看这个问题好三个小时,我已经厌倦了.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str [100];
char garbage[100];
int loopcondition = 0 , j = 0 , k = 0;
int userinput;
char a = "a" , e = "e" , i = "i", o = "o" , u = "u";
printf("!!!Hello World!!! Please type in a string from which I can remove all the vowels:\n");
//This program is supposed to take in a string and then delete all the vowels
gets(str); //gets user input
userinput = strlen(str); //makes userinput equal to the length of the string
while(loopcondition < userinput) //the loop runs until the condition is no longer smaller than the input
{
loopcondition++; //incrementation of condition
if(str[j] != a || e || i || o || u) // this should check wether the current position in the array is a vowel
{
garbage[k] = str[j] ; //if it's not the it puts the current character into the garbage array
k++; //iteration of the garbage array position
};
j++; //incrementation of string array position
};
garbage[k] = '\0';
strcpy (str , garbage ); //this copies the string?!?! aka reinitiliazing array variable
printf("%s" , str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您的char初始化应该是这样的
char a = 'a' , e = 'e' , i = 'i', o = 'o' , u = 'u';
Run Code Online (Sandbox Code Playgroud)
请记住,双引号(" ")用于字符串文字,单引号('')用于字符文字.
然后,
if(str[j] != a || e || i || o || u)
Run Code Online (Sandbox Code Playgroud)
这不是你||在c中使用逻辑OR()运算符的方式.链接是不可能的.你必须分别检查每个条件.就像是
if( (str[j] != a) &&
(str[j] != e) &&
(str[j] != i).......//so on
Run Code Online (Sandbox Code Playgroud)
但是,在我看来,如果你改变逻辑来使用switch案例,那将是一个更好的设计.
哦,并且更好地使用int main(void),这在标准中是推荐的.