# Trying to understand increment & decrement operators

This is a discussion on Trying to understand increment & decrement operators within the C Programming forums, part of the General Programming Boards category; K&R trying to explain increment & decrement operator through this illustration: Code: /* squeeze: delete all c from s */ ...

1. ## Trying to understand increment & decrement operators

K&R trying to explain increment & decrement operator through this illustration:

Code:
```/* 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++;
s[j] = '\0';
}```
I am really confused in this expression:

Code:
`s[j] = s[i];`
Elements are already in array, how are they manipulating it by changing index variables (i to j)?

2. Let's say the first three elements of the array have values 1, 2, and 3. Then assume j has a value of zero, and i has a value of 1. "s[j] = s[i]" therefore is equivalent to "s[0] = s[1]". So the array ends up containing the elements 2,2,and 3.

Your post causes unnecessary confusion on two fronts. Firstly, contrary to your subject line, your confusion about the expression has nothing to do with incrementing and decrementing. Second, the code you have shown would not work as you claim. I assume you have left out some braces - and, in doing so, completely changed the meaning of your code.

You might want to have a look at this link, concerned with asking intelligible questions.

3. Originally Posted by grumpy
Let's say the first three elements of the array have values 1, 2, and 3. Then assume j has a value of zero, and i has a value of 1. "s[j] = s[i]" therefore is equivalent to "s[0] = s[1]". So the array ends up containing the elements 2,2,and 3.

Your post causes unnecessary confusion on two fronts. Firstly, contrary to your subject line, your confusion about the expression has nothing to do with incrementing and decrementing. Second, the code you have shown would not work as you claim. I assume you have left out some braces - and, in doing so, completely changed the meaning of your code.

You might want to have a look at this link, concerned with asking intelligible questions.
I agree I messed up with the braces and the title. This was under captioned chapter hence I posted it that way. Sorry.

Code:
```void squeeze(char s[], char c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c){
s[j] = s[i];
j++;
}
s[j] = '\0';
}```
I am confused by how elements are being copied my changing indexes. For example, in the more advanced problem

Write an alternative version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.
I've seen a solution on web which is something like this:
Code:
``` #include <stdio.h>

void squeeze(char s1[], char s2[]);

int main(void)
{
char s1[] = "Augustx";
char s2[] = "st";

printf("%s", s1);
squeeze(s1,s2);
printf("\n%s", s1);
}

void squeeze(char s1[], char s2[])
{
int i, j, k;

for (i=0; s2[i] != '\0'; ++i)
{
for (j=k=0; s1[j] != '\0'; )
{
if (s1[j] != s2[i])
{
s1[k] = s1[j];
k++;
}
j++;
}

s1[k] = '\0';
}
}```
Here according to my understanding (which is obviously flawed):

i = 0; if s2[0] = 's' (first iteration) the output should be: "Augutx"
i = 1; if s2[1] = 't' (second iteration) the output should be: "Augusx" (Final Output)

I think because my understanding of copying elements within same array is wrong, I am having all the doubts?

4. Originally Posted by alter.ego
Code:
```void squeeze(char s[], char c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c){
s[j] = s[i];
j++;
}
s[j] = '\0';
}```
I am confused by how elements are being copied my changing indexes.
I suspect the problem is that you think all operations happen at once. The thing to remember is that things happen sequentially, one step after the other.

The assignment s[j] = s[i] only changes the value of a single character in the string. It does not change every character at once.

If i and j are equal, there is no effect (for example s[0] = s[0] does not change the value of s[0], which is the first character in the string).

If i and j are not equal, then s[j] = s[i] changes the single character identified as s[j]. So, if j is zero and i is 1, the assignment "s[j] = s[i]" is equivalent to doing "s[0] = s[1]". If the string is "ABC", the assignment s[0] = s[1] changes the string to "BBC". It does not change the string from "ABC" to to "BBB".

5. Great! Thank you very much. Got it.