Re: exercise 1-20 K&R



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.
.



Relevant Pages

  • Re: exercise 1-20 K&R
    ... of blanks to space to the next tab stop. ... int main is preferred. ... the next multiple of TABSIZE characters. ...
    (comp.lang.c)
  • Re: Tabs vs. Spaces
    ... void test(int j) ... With a tabsize of 2 this comes out as ...
    (alt.comp.lang.learn.c-cpp)
  • exercise 1-20 K&R
    ... of blanks to space to the next tab stop. ... #define TABSIZE 8 ... int counter, c; ...
    (comp.lang.c)
  • In a timeline pinch (Suspense: 25Jul05) pleading for assistance - Q1
    ... It will print out the computer's internal code for the character '7' ... It is never necessary to use an int for character processing. ... making an assignment to a pointer variable. ...
    (comp.lang.c)
  • [PATCH 36/70] ftdi_sio: Coding style
    ... unsigned short int divisor; ... /* Write an event character directly to the FTDI register. ... BmRequestType: 0100 0000B ... Configuration Descriptor ...
    (Linux-Kernel)