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?