Thread: Unable to pinpoint my mistake for a seemingly simple K&R exercise.

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

    Unable to pinpoint my mistake for a seemingly simple K&R exercise.

    I am trying to work on a problem mentioned in K&R book (Excercise 2-4).

    Exercise 2-4. Write an alternative version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.
    I am not writing a separate function, I am trying to achieve squeeze(s1,s2) by writing the code in the main program itself. Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    const char msg[15] = "blueberry";
    int c;
    int s[15];
    int j = 0;
    int i = 0;
    int k= 0;
    int ok = 0;
    
    
    for (i = j = 0; i<=15 && (c = getchar()) != '\0'; ++i)
    {
    	s[i] = c;
    	ok = 1;
    for (k=0; k <= 15; ++k)
    {
    	if (s[i] == msg[k]){
    	ok = 0;
    	}
    }
    if (ok == 1)
    {
    	s[j] = s[i];
    	j++;
    }
    }
    s[j] = '\0';
    
    for (j = 0; j<= 15; ++j)
    	printf ("%c ", s[j]);
    }
    Keyboard input:
    blackberry
    Desired output:
    u
    My output:
    l a c k b e r r y
    I've done several dry-runs but I cannot figure out where I am going wrong!? Why is control entering highlighted loop even if the condition is false? Extremely baffled! Please help.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The reason control enters the inner loop always is because the inner loop has to be executed before the outer loop has its next iteration. Imagine if I counted to ten three times.
    Code:
    for ( i = 0; i < 3; i++ ) {
       for ( cnt = 1; cnt <= 10; cnt++ ) {
          printf( "%d ", cnt ); 
       }
       printf( "\n" );
    }
    Just about the only way to do it, right? It works the same way no matter what the actual purpose is.

    You should probably get all of the input before you start to squeeze() it. Also be careful with arrays -- an arrays size is not a valid subscript.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks for the heads up. Basis your input I've tweaked the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    const char msg[15] = "blueberry";
    int c;
    int s[15];
    int j = 0;
    int i = 0;
    int k= 0;
    int ok = 0;
    int t = 0;
    
    
    for (i=0; i<=15 && (c = getchar()) != '\0'; ++i)
    {
    	s[i] = c;
    
    }
    
    for (t=j=0; s[t] != '/0'; ++t)
    {
           ok = 1;
    for (k=0; msg[k] != '/0'; ++k)
    
    {
    	if (s[t] == msg[k])
    	ok = 0;
    	
    }
    if (ok == 1)
    
    {
    	s[j] = s[i];
    	j++;
    }
    
    }
    s[j] = '\0';
    
            for (j = 0; j<= 15; ++j)
    	printf ("%c ", s[j]);
    }
    Can't say if I am getting the output . Also I couldn't quite understand the your array subset reservation?
    Last edited by alter.ego; 12-23-2011 at 01:33 PM.

  4. #4
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    I changed:
    Code:
    for (t=j=0; s[t] != '/0'; ++t)
    {
           ok = 1;
    for (k=0; msg[k] != '/0'; ++k)
    to:
    Code:
    for (t=j=0; s[t] != '\0'; ++t)
    {
           ok = 1;
    for (k=0; msg[k] != '\0'; ++k)
    and got it to compile. Not sure what you're supposed to do with it when you run it, but I figure you can go from there.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, I still see some mistakes:

    Code:
    for (i=0; i<=15 && (c = getchar()) != '\0'; ++i)
    When I told you before that the array size was not a valid subscript, I meant that i should not ever equal 15. There is no array element 15 because that is the size of s and msg: the actual elements are numbered from 0 to 14 inclusive. You make this mistake everywhere.

    The other possible mistake is that I'm not sure when getchar() is supposed to return '\0'. The K&R book teaches this idiom:

    Code:
    while((ch = getchar()) != '\n' && ch != EOF)
       ...
    That reads a line from standard input.

    After I fix these issues, and what andrew pointed out, I do get some output, but it doesn't look right. You'll just have to keep working at it.
    Last edited by whiteflags; 12-23-2011 at 06:19 PM.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Thanks for the help everyone. I have managed to get some output from the program. It helped me clear some concepts.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    const char msg[15] = "blueberry";
    int c;
    char s[15];
    
    int j = 0;
    int i = 0;
    int nomatch = 0;
    while (i < 15 && (c = getchar()) != '\n')
    {
    	s[i] = (char) c;
    	++i;
    }
    s[i] = '\0';
    printf("input: %s\n", s);
    printf ("value of i %d",i);
    printf("\nmessg: %s\n", msg);
    
    for (i=j=0; s[i] != '\0'|| msg[i] != '\0';)
    {
    	nomatch = 0;
    
    	if (s[i] != msg[i])
    		{
    			nomatch = 1;
    	}
    
    	if (nomatch == 1)
    	{
    		s[j] = s[i];
    		         j++;
    }
    
    	++i;
    }
    s[j] = '\0';
    printf ("output: %s		", s);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seemingly simple array program yielding bizarre output
    By alter.ego in forum C Programming
    Replies: 7
    Last Post: 04-07-2011, 11:15 AM
  2. Startup Lag: How Do I Pinpoint It?
    By Fried_Yoda in forum Tech Board
    Replies: 5
    Last Post: 10-30-2006, 06:21 AM
  3. VERY simple mistake
    By blankstare77 in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2005, 02:17 PM
  4. (!)Simple array exercise
    By Fizz in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2004, 12:45 PM
  5. what is causing this seemingly simple app to crash?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 08:36 PM