-
-
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; | |
} |
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
- the
nchars = 0
doesn't fix everything I think
I discussed it more thoroughly in my reply
http://forum.sa-mp.com/showthread.php?p=3993467#post3993467
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Thanks for the help man! After some discussion on CodeReview on StackExchange, I got some feedback with which I've made the 2nd revision.
A couple of comments just to see if we're on the same track:
while(nspaces > 0
, since we're decrementing it in steps of one, we're certain that nspaces must be 0.%
operand to calculate tabsizes, but the linenchars = 0;
works to fix the logic error where any line with TABSIZE (8) + nonwhitespace characters would be given extra tabs.continue
, but the approach in the 2nd version makes more sense to me than this one.Thanks a lot nonetheless, I am very happy you spent time in helping me :)