-
-
Save yugecin/7ff479178b8774f07e0ca1e92a70bd9b to your computer and use it in GitHub Desktop.
entab.c - Program that replaces a string of blanks by the min # of spaces & tabs required to generate identical output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://forum.sa-mp.com/showthread.php?t=649767 | |
#include <stdio.h> | |
#define TABSIZE 8 | |
int main() | |
{ | |
int c, cft, nspaces; | |
//cft = chars from tab, nspaces = number of spaces buffered | |
cft = nspaces = 0; | |
while((c = getchar()) != EOF) { | |
if(c == '\n') { | |
// reset ALL the things | |
cft = nspaces = 0; | |
putchar(c); | |
continue; | |
} | |
if(c == '\t') { | |
// treat tabs as spaces to easily minify later | |
// don't add TABSIZE, calculate the amount of chars to the next tab column | |
nspaces += TABSIZE - (cft + nspaces) % TABSIZE; | |
continue; | |
} | |
if(c == ' ') { | |
++nspaces; | |
continue; | |
} | |
//decide whether gap in between chars is long enough for a tab | |
while(nspaces >= TABSIZE - cft) { | |
putchar('\t'); | |
nspaces -= TABSIZE - cft; | |
cft = 0; | |
} | |
//insert all remaining spaces manually | |
while(nspaces > 0) { | |
putchar(' '); | |
--nspaces; | |
++cft; | |
} | |
cft = (cft + 1) % TABSIZE; | |
putchar(c); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh I guess you're talking about my code? yeah I removed that statement when I saw that laternspaces
can be negative because you check fornspaces >= TABSIZE - nchars
but subtract the fullTABSIZE
nchars = 0
doesn't fix everything I thinkI discussed it more thoroughly in my reply
http://forum.sa-mp.com/showthread.php?p=3993467#post3993467