exercise 1-20 K&R

Discussion in 'C Programming' started by Aenima1891, Jun 10, 2006.

  1. Aenima1891

    Aenima1891 Guest

    write a program that replaces strings of blanks by the minimum number
    of blanks to space to the next tab stop.

    #include <stdio.h>
    #define TABSIZE 8


    int main()
    {
    int counter, c;

    while ((c = getchar()) != EOF) {
    if (c == '\t') {
    for (counter = 1; counter <= TABSIZE; ++counter)
    putchar(' ');
    }
    else
    putchar(c);
    }
    return 0;
    }

    what do you think about this??
     
    Aenima1891, Jun 10, 2006
    #1
    1. Advertising

  2. Aenima1891 wrote:
    > write a program that replaces strings of blanks by the minimum number
    > of blanks to space to the next tab stop.
    >
    > #include <stdio.h>
    > #define TABSIZE 8
    >
    >
    > int main()


    int main (void) is preferred.

    > {
    > int counter, c;
    >
    > while ((c = getchar()) != EOF) {
    > if (c == '\t') {
    > for (counter = 1; counter <= TABSIZE; ++counter)
    > putchar(' ');


    This doesn't accomplish the goal of the exercise which is to replace
    the tab with the number of spaces needed for the *next* tab stop, that
    is, the next multiple of TABSIZE characters. Your version always
    prints TABSIZE spaces which will only be the right thing to do when the
    tab character is encountered immemdiately after a tab stop.

    > }
    > else
    > putchar(c);
    > }
    > return 0;
    > }
    >
    > what do you think about this??


    Robert Gamble
     
    Robert Gamble, Jun 10, 2006
    #2
    1. Advertising

  3. Aenima1891

    Michael Mair Guest

    Aenima1891 schrieb:
    > write a program that replaces strings of blanks by the minimum number
    > of blanks to space to the next tab stop.
    >
    > #include <stdio.h>
    > #define TABSIZE 8
    >
    >
    > int main()


    int main (void)

    > {
    > int counter, c;


    Note: It often helps understanding to separate variable declarations
    by "purpose" and not only by type. counter is used just as that,
    whereas c is intended to hold a character[*].
    This also makes for easier maintenance if you find out that the
    _guaranteed_ range of int is not sufficient for your purposes and
    you have to switch to long, unsigned long or size_t as counter type.
    int counter, anotherCounter;
    int c, anotherTempForCharacters;
    int resultOfSomeNoncounterCalculation;

    Some company coding standards even forbid declaring more than one
    variable per line if the declared variables are not loop variables.


    > while ((c = getchar()) != EOF) {
    > if (c == '\t') {
    > for (counter = 1; counter <= TABSIZE; ++counter)
    > putchar(' ');


    You misunderstood the purpose of the task.
    Every _line_ is to be formatted independently of the other. For every
    line, you start anew with tab stops. This means that you need a
    character count for the current line, not an overall character count.

    Tab stops are at the same character number (modulo TABSIZE) throughout
    the line, i.e. you insert space characters until your counter modulo
    TABSIZE equals that number.

    Example: For "#define TABSIZE 4",
    "\ta\tab\tabc\tabcd\tabcde\t\n" has to become either
    " a ab abc abcd abcde \n"
    ^ ^ ^ ^ ^ ^ ^ ^ ^
    where the second line indicates the tab stop positions for clarity.

    Or, if you admit the possibility of inserting 0 spaces to fit the
    description, your result would be
    "a ab abc abcdabcde \n"
    ^ ^ ^ ^ ^ ^ ^
    This is merely the difference between a while() and a do--while()
    loop (figure out which one for which case yourself) for you.

    > }
    > else
    > putchar(c);
    > }
    > return 0;
    > }
    >
    > what do you think about this??


    [*] To be more precise: A character cast to unsigned char or EOF.
    This is what you typically pass to putchar(), [f]putc() and the
    is....() functions and get from getchar(), etc.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jun 10, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    454
    John Saunders
    Aug 6, 2003
  2. lonelyplanet999

    Exercise needed for java 2 programmer test

    lonelyplanet999, Sep 30, 2003, in forum: Java
    Replies:
    1
    Views:
    4,186
    VisionSet
    Sep 30, 2003
  3. Xah Lee
    Replies:
    12
    Views:
    608
    Duncan Booth
    Jun 22, 2005
  4. Bruce .J Sam
    Replies:
    0
    Views:
    1,953
    Bruce .J Sam
    Jun 16, 2005
  5. Aries
    Replies:
    7
    Views:
    405
    Aries
    May 3, 2006
Loading...

Share This Page