Like Tree3Likes
  • 1 Post By Salem
  • 2 Post By Salem

Baffled By Question

This is a discussion on Baffled By Question within the C Programming forums, part of the General Programming Boards category; Whilst working my way through "The ANSI C Programming Language" I came across an exercise on chapter one that I ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    10

    Baffled By Question

    Whilst working my way through "The ANSI C Programming Language" I came across an exercise on chapter one that I just don't understand. The exercise is:
    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 a symbolic parameter?


    Could someone please explain to me what this is asking? Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Well you could do all your testing with
    Code:
    int main ( ) {
        int tabStops = 8;
        // your code
        return 0;
    }
    Then later on, perhaps enhance the code to be
    Code:
    int main ( int argc, char *argv[] ) {
        int tabStops = 8;
        // checking for command line params
        if ( strcmp(argv[i], "--tabs") == 0 ) {
            tabStops = // parse argv[i+1]
        }
        // your code
        return 0;
    }
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There are two questions:
    1. You need to write a program that replaces tab characters in the input file with some number of spaces, so that the next character appears at a multiple of n (i.e., a fixed set of tab stops every n columns).

    2. Should this n above be a variable of a symbolic constant (i.e., something that is read in from the user or something that is #defined in the code)?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    There is something rather freaky about member 'tabstop' answering questions about ... tab stops
    manasij7479 and TheBigH like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    1. You need to write a program that replaces tab characters in the input file with some number of spaces, so that the next character appears at a multiple of n (i.e., a fixed set of tab stops every n columns).
    Yeah, still a little confused...
    A little bit of research explained tab stops to me, but I don't understand what I'm supposed to do when a \t character is found. Replace it with n spaces, depending on where the tab stops that I defined are? So I'm redefining where all the tab stops in the document are?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ASCII View Post
    Yeah, still a little confused...
    A little bit of research explained tab stops to me, but I don't understand what I'm supposed to do when a \t character is found. Replace it with n spaces, depending on where the tab stops that I defined are? So I'm redefining where all the tab stops in the document are?
    Not at all. The file should look the same, visually. But instead of
    Code:
    →       Text starts here
    (where the arrow represents a single tab character) you should now have
    Code:
    ∙∙∙∙∙∙∙∙Text starts here
    (where each symbol represents a space).

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    ...
    I'm just replacing each tab with the appropriate number of spaces...

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right. Notice that that is not always n; if the tab had been after the word "Text" for instance, then you would only have done four spaces instead of eight (in this example), since the tabstop is still at column 8.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Yay it work:
    Code:
    #include<stdio.h>
    #define MAXLENGTH 1000
    #define TAB 8
    
    int Getline(char str[]);
    
    int main(void) {
    	char line[MAXLENGTH];
    	int len;
    	while((len = Getline(line)) != 0) 
    		printf("%s\n", line);
    	return 0;
    }
    
    int Getline(char str[]) {
    	int i;
    	int c;
    	for(i = 0; i < MAXLENGTH; i++)
    		str[i] = '\0';	
    	for(i = 0; ((c = getchar()) != '\n') && (i < MAXLENGTH); i++) {
    		if(c == EOF)
    			return 0;
    		if(c == '\t'){
    			int y;
    			for(y = i; y%TAB; y++)
    				str[y] = ' ';
    			i = y;
    		}
    		str[i] = c;
    	}
    	str[i] = '\0';
    	return i;
    }
    Last edited by ASCII; 08-28-2011 at 05:48 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yay it work:
    Now, on to the plural verb forms - Yah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 10:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 01:06 PM
  4. Newbie baffled
    By Steve MacD in forum C Programming
    Replies: 8
    Last Post: 02-17-2005, 10:44 AM
  5. baffled!
    By The Dog in forum C Programming
    Replies: 3
    Last Post: 07-03-2002, 06:58 PM

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