Skip to content

Instantly share code, notes, and snippets.

@raholland79
Created March 9, 2011 20:40
Show Gist options
  • Save raholland79/862963 to your computer and use it in GitHub Desktop.
Save raholland79/862963 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define WRAP 10
int main(void)
{
char buf[WRAP];
int bufpos = 0;
int linepos = 0;
int beg=0;
int last_whitespace = 0;
int flush = 0;
while ((buf[bufpos]=getchar())!=EOF)
{
// space left wrap-(pos-beg)
// line length pos-beg
// word length this_space - last space
if (buf[bufpos]==' ' || buf[bufpos] == '\n')
{
//if ((linepos-last_whitespace) > (WRAP-(linepos-beg)) && last_whitespace > 0 || buf[bufpos]=='\n') // word > space left
//{
// this line somehow updates bufpos?
// last run it cranked it from 4 to 10 and forced
// buf[last_whitespace] = '\n';
// flush=1;
//}
if (buf[bufpos]=='\n')
flush=1;
last_whitespace = linepos;
}
if (bufpos == (WRAP-1) || flush==1)
{
if (buf[bufpos]=='\n')
linepos=0;
else
++linepos;
buf[bufpos+1] = '\0';
if (last_whitespace>0)
buf[last_whitespace] = '\n';
printf("%s",buf);
if (!flush) {
putchar('\n');
beg=0;
last_whitespace=0;
}
bufpos=0;
flush=0;
for (bufpos=0;bufpos<WRAP;++bufpos)
buf[bufpos] = '\0';
bufpos=0;
} else {
++bufpos;
++linepos;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment