Ex. 2-4 KnR (2nd ed.) loop && array handling

This is a discussion on Ex. 2-4 KnR (2nd ed.) loop && array handling within the C Programming forums, part of the General Programming Boards category; Heyyy, I'm on exercise 2-4 in k&r 2nd ed. (Its getting kinda funky but I'm keeping with) Ex: Write an ...

  1. #1
    Insomniac
    Join Date
    Mar 2004
    Posts
    35

    Ex. 2-4 KnR (2nd ed.) loop && array handling

    Heyyy, I'm on exercise 2-4 in k&r 2nd ed.
    (Its getting kinda funky but I'm keeping with)

    Ex: Write an alternate version of squeeze(s 1, s2) that deletes each character in s1 that matches any character in string s2.

    I started off (trying) to copy every non-matching character into a third array. But the loop wasn't working.
    So now, I'm trying to fix my error and print a 'Z' insted of deleting.

    Code:
    void squeeze1(char s[], char t[]) {
            int i, j ;
    
            for (i = 0; s[i] != '\0'; i++) {
                    for (j = 0; t[j] != '\0'; j++)
                            if (s[i] == t[j])
                                    s[i] = 'Z';
                    }
            for (i = 0; s[i] != '\0'; i++)
                    printf("%c", s[i]);
            }
    My understanding is that the parent loop initiates, encounters the child loop, completes the child loop and exits (For each element in s, compare to every element in t).

    Can anyone show me where I've gone wrong. TIA

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    void squeeze2(char s1[], char s2[])
    {
      int i, j, k;
      int instr2 = 0;
    
      for(i = j = 0; s1[i] != '\0'; i++)
      {
        instr2 = 0;
        for(k = 0; s2[k] != '\0' && !instr2; k++)
        {
          if(s2[k] == s1[i])
          {
            instr2 = 1;
          }
        } 
    
        if(!instr2)
        {
          s1[j++] = s1[i];
        } 
      }
      s1[j] = '\0';
    }
    I got this from a web site with all the answers if you want it I can give it to you but i don't want to temp you by having it here if you want to figure it all out yourself
    [EDIT]your code does replace the correct characters with Z's my output
    Code:
    Enter 2 arrays
    cboard
    cprogramming
    ZbZZZd
    Last edited by linuxdude; 03-23-2004 at 08:15 PM.

  3. #3
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    your code does replace the correct characters with Z's my output
    I thought it should of done, but it just replaced every single letter, regardless of input. I've tried it again though (after another little rest) and it's working.

    In regards to the website, I already check another one out after, or during(if im struggling) an exercise, but I find the code boring and long. Not to mention that the style makes it look like an essay. They also cheat! ( )
    Note: It looks like the same site (users.powernet.co.uk)

    Thanks for the snippet as well, prompted me to put a little extra error checking in the getchar statements.

    Thanks for the help
    Last edited by olbas; 03-23-2004 at 10:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  2. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  3. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 06:26 PM
  4. Character in Array to end for loop.
    By mattz in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 10:05 AM
  5. Replies: 1
    Last Post: 11-19-2001, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21