简体   繁体   中英

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. If for example. Input string = "Hello" Specified char to be removed = "l" My output is "HeXXo". I seem to need to push the values in after removing the 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 . 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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