Skip to main content

1-22

Quest: page 34


Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n-=th colimn of input. Make sure your program does something intelligent wit hvery long lines, and if there are no blanks or tabs before the specified column.


  • read char in and count to the FOLD limit check if c is a newline character
  • i could add a word state here that long words aren't split up and always on one line.
#include <stdio.h>
#include <stdlib.h>

#define FOLD 80 // fold current line after x chars

int main() {
int c;
int cl=0;

while((c = getchar()) != EOF) {
if (cl == FOLD && c != '\n') {
putchar('\n');
cl=0;
}
putchar(c);
cl++;
if (c == '\n') {
cl= 0;
}
}
}