Hello guys i began reading K&R but there problem in a code i dont really understand it here code of a sequeeze function

/* squeeze: delete all c from s */
void squeeze(char *buffer, int c)
{
     int i, j;
     for (i = j = 0; buffer[i] != '\0'; i++) {
     if (buffer[i] != c) 
         buffer[j++] = buffer[i];
     buffer[j] = '\0';
     }
}
int main(void)
{
    char name[]="NAME";
    puts("Name Before Sequueze: \n");
    puts(name);
    squeeze(name,'N');
    puts("Name After Sequueze: \n");
    puts(name);
    getchar();
    return 0;
}

i dont really understand the code its the same string and everything how if i made 1st character whish is N it wont be delted i dont really quite get that thanks.

Recommended Answers

All 7 Replies

>i began reading K&R ...
Well, let's read the next sentence after squeeze function definition:

Each time a non-c occurs, it is copied into the current j position, and only then is j incremented to be ready for the next character. This is exactly equivalent to

if (s[i] != c) {
       s[j] = s[i];
       j++;
   }

What else?
Better turn off your computer, take a sheet of paper and a pencil then try to squeeze a simple string (do it step by step)...

yah i will do that but srry 1 question why when i do buff=0; i get NMEE but when buff[j]=0; i get normal result why ?

Sorry, I don't understand:
>why when i do buff[i]=0; ...
No such assignment in the squeeze function!
WHY and WHERE you do buff[i]=0 ?!

no i mean when i do buff=0 i get completly diff answer when i do buff[j]=0 so i was asking why do i Get it when i do that ...

also i tried to write in txt there something is weird about this

/* squeeze: delete all c from s */
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];
             j++;}
         printf("%d",j);
         s[j] = '\0';
}
int main(void)
{
    char name[]="NAME";
    puts("Name Before Sequueze: \n");
    puts(name);
    squeeze(name,'N');
    puts("Name After Sequueze: \n");
    puts(name);
    getchar();
    return 0;
}

you see printf it prints the %d whish is 3 then it should remove last character not the first one ? because we did the the if inside the for statement and the removing of the character outside it whish should increment the j to 3 whish should remove E

lolguy> the removing of the character outside it whish should increment the j to 3 whish should remove E

You are confused. printf("%d",j); is not the subscript used to delete the matched character. for (i = j = 0; s[i] != '\0'; i++) means, walk through the string until you find the string terminator if (s[i] != c) as we walk through the string, if the present char s doesn't match the given character to be deleted, then and only then s[j] = s[i]; which keeps track of the all new and improved string. After that the work jump to the next char by j++; s[j] = '\0'; makes sure that in case the loop finished with the original string, the new one is properly terminated and therefore a valid string as well.

commented: break it down! +9

ohhhh i get it now lol i was confused thanks alot mate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.