删除字符串中的元音。我究竟做错了什么?

Deleting vowels in a string. What am I doing wrong?

我试图通过将所有辅音放入不同的数组然后将第一个数组重新初始化为第二个数组来删除字符串的所有元音。之后应该打印第一个数组,即只打印辅音。真不知道问题出在哪里,我已经看这个问题整整三个小时了,实在是烦死了。

#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] = '[=10=]';
    strcpy (str , garbage ); //this copies the string?!?! aka reinitiliazing array variable
    printf("%s" , str);
    return 0;
}

您的 char 初始化应该类似于

char a = 'a' , e = 'e' , i = 'i', o = 'o' , u = 'u';

请记住,双引号 (" ") 用于 字符串文字 单引号 ('') 用于字符文字.

然后,

  if(str[j] != a || e || i || o || u)

这不是您在 c 中使用逻辑或 (||) 运算符的方式。链接是不可能的。您必须分别检查每个条件。像

if( (str[j] != a) && 
                     (str[j] != e) &&
                                      (str[j] != i).......//so on

但是,在我看来,如果您更改逻辑以使用 switch 案例,这将是一个更好的设计。

哦,最好使用标准中推荐的 int main(void)

char str [100];
char *src;          // source position of copying characters
char *dst;          // destination position 

...........

gets(str); //gets user input
printf( "Input: %s\n" , str);

for( dst = src = str; *src; src++ )       // scan chars of the string
{
    if( *src != 'a' && *src != 'e' &&     // check if a vowel
        *src != 'i' && *src != 'o' && *src != 'u')
    {
        * dst ++ = * src;                 // no? copy it
    };
}
*dst = 0;  // ASCII NUL (zero) char to terminate the string

printf( "Result: %s\n" , str);

for 循环使用 src 指针扫描整个字符串,从头开始 (str) 直到结束(零字符,ASCII NUL,由*src表达式,相当于*src != 0).

每个字符都经过测试,如果它不是元音字母,则将其复制到 dest 位置,然后向前推进一个 (* dst ++ = * src)。 dest 变量最初设置为 str,因此除了元音字母外,所有字符都在字符串的开头部分被一个接一个地复制。最后一个赋值 (*dst = 0) 终止字符串。

使用 strchr:

char vowels = "aeiou";
if (strchr(vowels, 'x'))
 // Then 'x' is a vowel.

你一次要检查 1 个字符是否是元音字母,所以不要使用(不安全的)gets() 或根本不使用多个数组,你可以只使用 getchar()并且只将非元音字母添加到您的数组中。这简化了其余逻辑,因此您可以只使用指针。

#include <stdio.h> //for puts and getchar
int main(void){
  char s[128], *sp = s, *end = sp + sizeof(s) - 1;
  puts("Please type in a string");
  while ((*sp=getchar())!='\n' && sp<end){
    switch(*sp|32){ // |32 will catch uppercase vowels too
    case 'a': case 'e' : case 'i' : case 'o' : case 'u' :
      continue; //*sp will be overwritten
    default :
      sp++;
    }
  }
  *sp=0;
  puts(s);
}

或使用strch()

#include <stdio.h> //for puts and getchar
#include <string.h> //for strchr
int main(void){
  char s[128], *sp = s, *end = sp + sizeof(s) - 1;
  puts("Please type in a string");
  while ((*sp=getchar())!='\n' && sp<end)
    if (!strchr("aeiouAEIOU",*sp)) sp++;
  *sp=0;
  puts(s);
}