Re: exercise 1-20 K&R



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

.



Relevant Pages

  • A source file de-tabifier for you
    ... *transform* one tab setting to another; thus, you can write your code with ... void do_line; ... int reset_on_com = FALSE; ... char **argv; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: newbie question
    ... int main{ ... The problem is that some news readers clients (or is it some news ... server, I don't know) strip the TAB and consequently, your code appears ...
    (comp.lang.c)
  • Re: exercise 1-20 K&R
    ... of blanks to space to the next tab stop. ... int counter, c; ... character count for the current line, ... Tab stops are at the same character number (modulo TABSIZE) throughout ...
    (comp.lang.c)
  • Re: I dont quite understand this exercise...
    ... >ask the user/get and set a variable, say n, and replace all tabs with n ... A text file containing some tab characters. ... if N is 4 and the original file is: ... int jj = 0; ...
    (comp.lang.c)
  • [patch 1/14] s3c2412/s3c2413 support
    ... +s3c_lookup_cpu(unsigned long idcode,int alt) ... struct cpu_table *tab; ... int count; ...
    (Linux-Kernel)