entab

tooltiperror

Super Moderator
Reaction score
231
I'm writing a utility [LJASS]entab[/LJASS]. I'm compiling it with gcc.

entab takes input. Because tabs are 8 cells wide, it replaces eight spaces with [LJASS]"\t"[/LJASS]. But it's... not working. I run it like this:

JASS:

$ gcc -o entab entab.c
$ ./entab
> a  a
  a  a
> a        a
  a      a


It's just working correct. I've tried modifying it a lot but it's not working. Any help?

Code:
#include <stdio.h>

int main()
{
	int c, spaces;

	spaces = 0;

	while ((c = getchar()) != EOF) {
		if (c == ' ') {
			spaces++;
			if (spaces == 8) {
				putchar('\t');
				spaces = 0;
			}
		}
		else {
			for ( ; spaces != 0; spaces-- ) {
				putchar(' ');
			}
			putchar(c);
		}
	}

	return 0;
}
 

lep

Active Member
Reaction score
8
Tabs are no replacements for a fixed amount of spaces. They align at multiple of the tab-width-amount (normally 4 or 8).
Code:
a	a
aa	a
aaa	a
aaaa	a
aaaaa	a
aaa	a	a
aaaa	aaa	a

Spoiler:
So, if you want to replace spaces with tabs, you have to count the position where the next non-space character appears and check if its position is a multiple of your tab-length.
 

tooltiperror

Super Moderator
Reaction score
231
Ah, I see. I was under the impression that the tabs were just spaces. Thanks, I should be able to fix it now.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top