Thread: Trying to understand increment & decrement operators

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Quote Originally Posted by grumpy View Post
    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. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by alter.ego View Post
    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".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Great! Thank you very much. Got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. increment and decrement operators in c
    By Abhishek Thakur in forum C Programming
    Replies: 7
    Last Post: 07-05-2012, 04:25 AM
  2. Increment / Decrement Operators
    By capricorn in forum C Programming
    Replies: 1
    Last Post: 12-16-2010, 09:35 PM
  3. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  4. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM
  5. increment/decrement operators
    By ZakkWylde969 in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2003, 04:17 PM