:: Re: [DNG] Studying C as told. (For …
Góra strony
Delete this message
Reply to this message
Autor: Edward Bartolo
Data:  
Dla: KatolaZ
CC: dng
Temat: Re: [DNG] Studying C as told. (For help)
Hi,

I am using nano. I paste the program's output in nano and use the
cursor keys to investigate the position of tabs.

This is the final debugged version of the tab insertion program. It
assumes tabs are 8 characters apart with the first column having no
tab.


#include <stdio.h>

/* insert tabs instead of repeated spaces */

int tab_width = 8;
int col = 0;

int is_tab_stop(int col) {
return (col % tab_width == 0 && col > 1);
}

void replace_spaces_with_tabs(char c) {
  int count = 0;
  if (c == ' ') {
    ++count;


    while((c = getchar()) == ' ') {
      ++col;
      ++count;
      if (is_tab_stop(col)) {
        putchar('\t');
        count = 0;
      }
    }
  }


  if (count > 0)
    while(putchar(' ') && --count);


putchar(c);
}

int main() {
char c;

  while((c = getchar()) != EOF) {
    ++col;
    replace_spaces_with_tabs(c);
  }


return 0;
}

On 23/06/2016, KatolaZ <katolaz@???> wrote:
> On Thu, Jun 23, 2016 at 02:17:37PM +0200, Edward Bartolo wrote:
>> Hi,
>>
>> I am doing exercise 1-21 Pg 48 The C programming language" (Kernighan
>> & Ritchie). Since I am finding difficulty inspecting tabs in XFCE4.10
>> terminal, I would like someone to inspect this program which replaces
>> repeating spaces with tabs when there are sufficient consecutive
>> spaces. I am not asking for a long reply. A short reply giving me
>> feedback as to whether the code is good or not, is enough.
>>
>
> "hexdump" will help you understand if you have tabs in the right
> places. Tab is ASCII character 9.
>
> HND
>
> KatolaZ
>
> --
> [ ~.,_  Enzo Nicosia aka KatolaZ - GLUGCT -- Freaknet Medialab  ]
> [     "+.  katolaz [at] freaknet.org --- katolaz [at] yahoo.it  ]
> [       @)   http://kalos.mine.nu ---  Devuan GNU + Linux User  ]
> [     @@)  http://maths.qmul.ac.uk/~vnicosia --  GPG: 0B5F062F  ]
> [ (@@@)  Twitter: @KatolaZ - skype: katolaz -- github: KatolaZ  ]
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng

>