Until recently, I decided to go back to Chapter 1 of the K&R Book, Edition 2, to try to "improve" the code I've already done. I have also made some changes since I was limited to just using what they taught in each section of Chapter 1. Especially the following exercise:
Exercise 1-22. 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 column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
Here is my solution to the exercise above:
#include <stdio.h>
#include <string.h>
#define DEFLINEWIDTH 80 /* DEFAULT LINE WIDTH */
#define BUFSIZE 1024
inline int isblank(int c);
/* fold long input lines */
int
main(void)
{
char buf[BUFSIZE];
int i, indx;
int width, space;
int ch;
width = DEFLINEWIDTH;
indx = 0;
while ((ch = getchar()) != EOF) {
if (ch == '\n') {
printf("%.*s\n", indx, buf);
indx = 0;
}
for (i = indx; i >= 0 && !isblank(buf[i]); i--)
;
space = i;
if (space >= width && space != -1) {
printf("%.*s\n", space, buf);
memmove(buf, buf + space, indx - space);
indx -= space;
}
buf[indx++] = ch;
}
return 0;
}
inline int
isblank(int c)
{
return c == ' ' || c == '\t';
}
Which I would like to know how to improve it, even if it's just a little bit.
- Sample Input
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
- Sample Input
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
- Sample Input
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
- Sample Input
Write a program to "fold" long input lines in two or more shorter lines after the non-last blank character that occurs before the n-th columns of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
Note: I intended the results to be somewhat similar to the "fold" command. (though unfortunately, I couldn't get them to be exact).
By the way, I tried to show the differences between both results using "vimdiff". The ones on the right are the results of the program I wrote.
fold
command is not what you want to compare with - you really want to produce output similar to whatfmt
produces. \$\endgroup\$