Exercise 1-22 (Fold, wrap lines)

Chapter_1     Exercise_1-21 Exercise_1-23







Exercise 1-22     K&R, p. 34


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.




fold.c         download


#include <stdio.h> // for getchar(), printf(), EOF

#define FOLDLINE 80 // 80 characters
#define MAXWORD 10 // search for gap (whitesp. or punct.) before end of line
#define TRUE 1
#define FALSE 0

int insideWord = FALSE; // global variable, used to signal when a line
// does not end with whitespace or a punctuation mark or sign
int getLine(char line[], int maxline); // getline() is defined by stdio.h
// int getLine(char [], int); // alternative declaration

int main()
{
char c;
int i; // counter
int len; // current line length
char line[FOLDLINE+1]; // +1 for ending '\0'
char word[MAXWORD+1]; // when line doesn't end with whitesp. or punct. sign
int wordlen = 0; // length of word printed on the next line, after
// splitting line[] in two at a gap (whitesp. or punct.)
while((len = getLine(line, FOLDLINE+1-wordlen)) > 0) // outer while()
{ // line containing only '\n' not empty
if (insideWord) // insideWord == 1 (insideWord != 0)
{
wordlen = 0; // reset
while (wordlen < MAXWORD && insideWord) // inner while()
{
c = line[len-1];
if (c == ' ' || c == '\t' || c == ',' || c == ';' ||
c == '.' || c == ':' || c == '!' || c == '?')
{insideWord = FALSE;}
else {len--, wordlen++;}
}
if (insideWord) // could not find a gap to split the line[]
{
printf("%s\r", line); // fold line
wordlen = 0; // reset
}
else // found a gap, split line[]
{
for (i = 0; i < wordlen; i++)
{word[i] = line[len+i];}
word[wordlen] = '\0'; // properly end string
line[len] = '\0'; // reset line to shorter length
printf("%s\n", line); // fold line (split)
printf("%s", word); // rest of initial line
}
}
else // getLine() ended with EOF, '\n' or else
{ // line ended with whitespace or punctuation
printf("%s", line);
wordlen = 0; // reset
}
}

return 0;
}

// getLine(): read a line into s[], return length
int getLine(char s[], int lim)
{
int c = EOF; // initialize
int i;
// getchar() is only executed if (i < (lim-1)):
for (i = 0; i < (lim-1) && (c = getchar()) != EOF && c != '\n'; i++)
{ // from 0 to lim-2; s[lim-2]='\n' or not, s[lim-1]='\0'
s[i] = c;
}
if (i == lim-1) // c != EOF, c != '\n'
{
if (c != ' ' && c != '\t' && c != ',' && c != ';' &&
c != '.' && c != ':' && c != '!' && c != '?')
{insideWord = TRUE;} // line doesn't end with whitespace or punct. mark
else {insideWord = FALSE;} // reset
}
if (c == '\n') // i < (lim-1), getchar() executed
{
s[i] = c; // '\n'
i++;
}
s[i] = '\0'; // the null character ends a string

return i; // max(i) == (lim-1), assuming (lim-1) > 0
}
/*
gcc fold.c -o fold
./fold // input from the keyboard

./fold < fold.c // input from source file

./fold < fold.c > copy.c // input fold.c, output copy.c
meld fold.c copy.c

./fold < fold // input from binary file

rm copy.c // clean
*/





Note:  You can test the program with input fold.c and other values for FOLDLINE, like 30 or 40.









Chapter_1     Exercise_1-21 BACK_TO_TOP Exercise_1-23



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo