0
#include <stdio.h>

void squeeze(char s[], int c)
{
int i, j;
for(i = j = 0; s[i]!= '\0'; i++)
{       
    if (s[i] != c)
    {
        s[j++] = s[i];
    }
}   
s[j] = '\0';
printf("%s\n", s);
}


int main(void)
{
squeeze("asfafsdfsaaaasdfsd", 'a');
}

I run this program in mac, and it looks like the problem comes from the line: s[j++] = s[i]

but I don't know why.

Thanks!

Forrest
  • 51
  • 1
  • 9

1 Answers1

1

Because string literals are read-only. To make a writable array of characters, try this

int main( void )
{
    char array[] = "asfafsdfsaaaasdfsd";
    squeeze( array, 'a' );
}
user3386109
  • 34,287
  • 7
  • 49
  • 68