简体   繁体   English

如何从字符串中删除所有出现的特定字符?

[英]How do I remove all occurrences of a specific char from a string?

Hi im attempting to remove a char from a C string, but the output doesnt seem correct. 您好我试图从C字符串中删除一个字符,但输出似乎不正确。 If for example. 例如,如果。 Input string = "Hello" Specified char to be removed = "l" My output is "HeXXo". 输入字符串=“Hello”要删除的指定字符=“l”我的输出是“HeXXo”。 I seem to need to push the values in after removing the char? 我似乎需要在删除char之后推送值?

Code below: 代码如下:

#include <stdio.h>
#include <stdlib.h>

void squeeze(char str[], char c);

void main (){
  char input[100];
  char c;
  printf("Enter string \n");
  gets(input);
  printf("Enter char \n");
  scanf("%c", &c);
  printf("char is %c \n", c);
  squeeze(input , c );

  getchar();
  getchar();
  getchar();
}

void squeeze(char str[], char c){
    int count = 0, i = 0;   

    while (str[count] != '\0'){
      count++;
    }

    printf("Count = %d  \n", count);
    for ( i = 0 ; i != count; i++){
      if (str[i] == c){
            printf("Found at str[%d] \n", i);
            str[i] = "";
      }
    }
    printf(" String is = %s", str);
}
 str[i] = ""; 

You are trying to assign a pointer instead of a character. 您正在尝试分配指针而不是字符。 You probably meant ' ' but that's not the right way to delete characters from a string either, that's replacing them. 你可能意味着' '但这不是从字符串中删除字符的正确方法,而是取代它们。 Try: 尝试:

char *p = str;
for (i = 0 ; i != count; i++) {
    if (str[i] != c)
        *p++ = str[i];
}
*p = 0;

EDIT 编辑

Here is a solution that I like more: 这是我更喜欢的解决方案:

char *p = s; /* p points to the most current "accepted" char. */
while (*s) {
    /* If we accept a char we store it and we advance p. */
    if (*s != ch)
        *p++ = *s;

    /* We always advance s. */
    s++;
}
/* We 0-terminate p. */
*p = 0;
#include <stdio.h>
#include <stdlib.h>

void squeeze(char str[], char c);

int main ()
{
    char input[100];
    char c;
    printf("Enter string \n");
    gets(input);
    printf("Enter char \n");
    scanf("%c", &c);
    printf("char is %c \n", c);
    squeeze(input , c );

    return 0;
}

void squeeze(char str[], char c){
    int count = 0, i = 0,j=0;
    char str2[100];
    while (str[count] != '\0'){
        count++;}

    printf("Count = %d  \n", count);
    for ( i = 0,j=0 ; i != count; i++){
        if (str[i] == c)
        {
            printf("Found at str[%d] \n", i);
            //    str[i] = '';
        }
        else
        {
            str2[j]=str[i];
            j++ ;
        }
    }

    str2[j]='\0' ;
    printf(" String is = %s", str2);
}

This is the modified version of your code. 这是您的代码的修改版本。 I've created a new array and placed the rest of the non-matching letters into it. 我创建了一个新数组,并将其余的非匹配字母放入其中。 Hope it helps . 希望能帮助到你 。

With

str[i] = "";

you assign the address of the string literal to the char at position i . 您将字符串文字的地址分配给位置ichar First of all you should get a warning for that by the compiler because the types are not really compatible; 首先,你应该得到编译器的警告,因为类型并不真正兼容; secondly, you would need to either assign a replacement character there, eg 其次,你需要在那里分配替换字符,例如

str[i] = '_';

or actually remove them by shifting all later characters back (thus overwriting the character to replace). 或者实际上通过将所有后来的字符移回来移除它们(从而覆盖要替换的字符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM