This is Exercise 1-21 of K&R (2nd edition).
I've seen someone else's post about it but it doesn't solve the problem the way my code does, and I wasn't sure whether I was allowed to post my code as an answer and ask for feedback (it just seemed better to create a new thread and ask for advice here, didn't wanna pollute the other user's topic but if that's what I am supposed to do please let me know and I'll reply over there).
In any way, here's the approach I took to solve the problem:
Approach:
We'll keep track of number of spaces encountered (between nontab/nonspace characters)
We'll keep track of characters (that aren't tabs/blanks/newlines) per input line
We'll evaluate the "gaps" generated by spaces by:
Evaluating whether the number of spaces in between those characters.
A gap will be "big enough" when the number of spaces is >= TABSIZE
Then, for all the left over spaces in our "buffer", we'll print them out individually
Finally, we print out the character that was read in (which was not a tab/blank)
As well as updating space count and character count if necessary.
Here's the code:
#include <stdio.h>
//Entab: replaces string of blanks by the min # of spaces & tabs
//Same tab spots as for detab (8)
//Given performance to tabs.
#define TABSIZE 8
//Approach:
//We'll keep track of number of spaces encountered (between nontab/nonspace characters)
//We'll keep track of characters (that aren't tabs/blanks/newlines) per input line
//We'll evaluate the "gaps" generated by spaces by:
// evaluating whether the number of spaces in between those characters.
// A gap will be "big enough" when the number of spaces is >= TABSIZE
// Then, for all the left over spaces in our "buffer", we'll print them out individually
//Finally, we print out the character that was read in (which was not a tab/blank)
//Update space count and character count if necessary.
int main()
{
int c, nc, ns;
nc = ns = 0;
while((c = getchar()) != EOF)
{
//if this character was a space
if(c == ' ') {
//increase the space count
++ns;
}
//else if the character read in is not a space
else {
//while one or more spaces are still "in buffer"
while(ns > 0)
{
//decide whether gap in between chars is long enough for a space
if(ns >= TABSIZE - nc)
{
//if it is, then place a tab, and remove that amount of spaces
putchar('\t');
ns -= TABSIZE;
}
//else, if the gap is not big enough for spaces, just print them out individually
else
{
putchar(' ');
--ns;
}
}
//if read a newline, reset char count
//if not, increase it by one for the char we just read in
//as well as update the total space count to 0
if(c == '\n')
nc = ns = 0;
else if(c != '\t')
++nc;
//finally, put this "nonspace/nontab" character just read
//after the gap we just covered
putchar(c);
}
}
}
Also, keep in mind that I am just revisiting C's K&R and I am aware that some of it may be outdated, but I'm doing my best to use what's still "useful" of the book and discard what may not be.
I'm using these notes to accompany my travels through C.
detab()
is K&R exercise 1-20. \$\endgroup\$