Thread: squeeze() function

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    squeeze() function

    Code:
    void squeeze(char s1[], char s2[])
    {
       int i,j,k;
       
       for(i = k = 0; s1[i] != '\0'; i++)
       {
          for(j = 0; s2[j] != '\0' && s2[j] != s1[i]; j++)
             ;
          if (s1[j] == '\0')
             s1[k++] = s1[i];
       }
    
       s1[k] = '\0';
    }
    im havin trouble understanding this piece of code. i know what it does. it deletes, in s1[], whatever occurs in s2[]. ive been at this k&r exercise for a while and cheated by looking at the answer book. yet i still dont understand the concept of this function. the for loop, i know, steps through each array element and tests. and while s2[j] != s1[i], j keeps incrementing. but once it reaches a match, what happens?
    Code:
    if (s2[j] == '\0')
       s1[k++] = s1[i];
    if it reaches a match before the actual '\0' what happens? thanks in advance for response. and sorry if this post seemed a little long-winded. just trying to understand everything. =)

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    That solution doesn't appear to work (try compiling and running it yourself).

    A quick search found this solution.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    i guess this piece of code works if s2 > s1 and the similar chars are found within s1 length

    let s1=hello and s2=bull$$$$
    As the for loop checks for s1[i] j will end up with a number more than length of string s1
    so untill there is a match k keeps on increasing
    and assigns the chars to s1[k++]

    now when the match l is found j=2 as s1[2]!='\0'
    nothing happens and loop iterates..like this the chars ll are skipped and when o is checked s1[2]is
    assigned o;
    uot of the loop string s1 is terminated after k chars

    i think u got it dude......

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>>let s1=hello and s2=bull$$$$ <<<
    (I'm presuming you're talking about the OPs code)
    Let's run that example:
    Code:
    int main(void)
    {
        char    a1[] = "hello";
        char    a2[] = "bull$$$$";
    
        squeeze(a1, a2);
    
        printf (">a1:%s<\n", a1);
        printf (">a2:%s<\n", a2);
    
        return 0;
    }
    
    /*
     Output:
     
     >a1:h<
     >a2:bull$$$$<
     
     */
    The output is broken, and so is the code.

    Note: The "bull$$$$" was the real word in my code. If you actually use $ signs, the output is:
    >a1:<
    >a2:bull$$$$<
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    still puzzled.

    the two for loops loop on each array element until '\0' is encountered or a match is found between s2[] and s1[]. when i match is found it breaks out of the loop right?

    the if statement:
    Code:
    if (s2[j] == '\0')
       s1[k++] = s1[i];
    will only execute if s2[j] equals the null character. so if it finds a match before the actual '\0' the if statement wont execute. im lost. =\

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    if the match is not found before the null whatever chars r there in s1 get assigned as s1[k++]
    when there is a match found before null then that char is missed
    in assigning to s1[k++]
    so after the loops r over s1[k] is set to null so the string teminates there itself

    for better understanding u do the itewrations manually taking s1 hello s2 as nullchar

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>still puzzled.
    You do understand what I'm saying, right? The original code you posted doesn't appear to work properly, so trying to learn from it ain't a good idea, unless you're looking to find how it's broken.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    the piece of code works for my system what do mean by OPs code
    u try taking s1 s2 as fixed array of length 10

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OP = Original Poster (thread starter)

    It's broken in that it requires both arrays to be the same length. I was assuming that you wouldn't want that, as you'd most likely want to do something like:

    squeeze(myarray, "abc"); /* remove abc from myarray */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    thanks for the responses everyone.

    Code:
    for(i = k = 0; s1[i] != '\0'; i++)
       {
          for(j = 0; s2[j] != '\0' && s2[j] != s1[i]; j++)
             ;
          if (s1[j] == '\0')
             s1[k++] = s1[i];
       }
    it compares s1[i] to s2[j] when the 2nd for loop reaches either '\0' or a match it goes to the if statement. the if statement then checks to see if the current j array element is '\0' if it is a '\0' then no match was found and it is copied to s1. now i understand! =)

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Code:
    if (s1[j] == '\0')
             s1[k++] = s1[i];
    I think the problem is there no? (didn't try to run it)
    it should be:
    Code:
    if (s2[j] == '\0')
             s1[k++] = s1[i];
    It's s2 you are parsing in the nested for loop. If s2[j] is NUL then it means there was no match, hence you should keep s1[i], else you should discard it.

    s1[j] on the other hand could be out of bound or whatever...

  12. #12
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    Originally posted by P.Phant
    it should be:
    Code:
    if (s2[j] == '\0')
             s1[k++] = s1[i];
    It's s2 you are parsing in the nested for loop. If s2[j] is NUL then it means there was no match, hence you should keep s1[i], else you should discard it.

    s1[j] on the other hand could be out of bound or whatever...
    yeah. youre right. my code is a typo. had to type it out because the actual file is on my linux box. thanks for pointing that out. =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM