Thread: Kernigan and Ritchie Exercise 5-11

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    69

    Kernigan and Ritchie Exercise 5-11

    Here is the description: Modify the program detab (written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments.

    What I know about tabs, when I hit a tab I travel to the next tab stop. For example, assume tab stops (8, 16, 24, 32) etc so if I'm in column 12 and hit a tab stop I go to column 16. That is, what this exercise is looking for I assume.

    Just now I'm having hard time figuring out how should I do this, any pseudocode/other help is appreciated.

    Here is my version of detab, by the way, if it is any help here:

    Code:
    #include <stdio.h>
    #define MAXLINE 1000
    #define SPACE  ' '
    #define TAB  '\t'
    
    
    /* Write a program detab that replaces tabs in the input
    with the proper number of blanks to space to the next tab stop. 
    Assume a fixed set of tab stops, say every n columns. Should n be
    a variable or symbolic constant? */
    
    
    int countspaces(int offset, int tabsize)
    {
    
    
      return tabsize - (offset % tabsize);
       
    }
    
    
    
    
    int main(void)
    
    
    {
      
      int pos, c;
    
    
      while((c = getchar()) != EOF)
      {
        
        
      
      if(c == TAB) 
            {
                int numSpaces = countspaces(pos, 5);   /* n is a symbolic constant here, not a variable */
                for(int i = 0; i < numSpaces; ++i)
                {
                    putchar(SPACE);
                    ++pos;
                }
            }
            
        else if( c == '\n')
            {
             
              putchar(c);
              pos = 0;
              
            }
            
        else
          {
             putchar(c);
             ++pos;
          }
       }
      
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well it's looking for you to turn main into
    int main ( int argc, char *argv[] )

    And for you to run the program with a command line parameter, say
    ./mydetab --stops="8,16,24,32,40" filename

    Which would be current behaviour, or
    ./mydetab --stops="5,10,15,20,25,30" filename

    So if you were at column 12, then the next stop would be 16 in the first case, and 15 in the second.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    69
    Thank you @Salem. Can you be more detailed about the code that should be written? It's a bit of time I have written the above code, and my C programming is also rusty (Not to mention the breaks that I have kept from time to time).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dennis M. Ritchie
    By kermit in forum General Discussions
    Replies: 41
    Last Post: 10-17-2011, 04:39 PM
  2. Kernighan and Ritchie: Gods
    By Vespasian in forum Programming Book and Product Reviews
    Replies: 4
    Last Post: 09-25-2011, 11:41 PM
  3. Kernighan and Ritchie confusion
    By suchthefool in forum C Programming
    Replies: 3
    Last Post: 02-06-2011, 11:16 AM
  4. Kerningham Ritchie - example query
    By aynzoya in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 01:25 PM
  5. Kernigan and Ritchie Book Question!
    By Matus in forum C Programming
    Replies: 10
    Last Post: 11-06-2008, 11:58 AM