A
aegis
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.
so we ensure that no blanks or tabs are the last character
at column_max - 1?
but if there are, what should we do?
what if the column width was set to 10
then the sentence:
"beautiful is a nine letter word"
the output should look like?
beautiful\n
is a nin\n
e letter\n
word\n
right?
here is my solution but I would like to know if it is correct
I can't make sense of the vaguely worded problem statement.
#include <stdio.h>
#define COLUMN 25
int main(void)
{
int c;
int i = 0;
int bt = 0;
while((c = getchar()) != EOF) {
if(i == (COLUMN - 1) && c == ' ' || c == '\t') {
++i;
++bt;
} else if(i == COLUMN) {
putchar('\n');
i = 0;
}
if(i < COLUMN && !bt) {
putchar(c);
++i;
} else if(bt) {
bt = 0;
}
}
return 0;
}
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.
so we ensure that no blanks or tabs are the last character
at column_max - 1?
but if there are, what should we do?
what if the column width was set to 10
then the sentence:
"beautiful is a nine letter word"
the output should look like?
beautiful\n
is a nin\n
e letter\n
word\n
right?
here is my solution but I would like to know if it is correct
I can't make sense of the vaguely worded problem statement.
#include <stdio.h>
#define COLUMN 25
int main(void)
{
int c;
int i = 0;
int bt = 0;
while((c = getchar()) != EOF) {
if(i == (COLUMN - 1) && c == ' ' || c == '\t') {
++i;
++bt;
} else if(i == COLUMN) {
putchar('\n');
i = 0;
}
if(i < COLUMN && !bt) {
putchar(c);
++i;
} else if(bt) {
bt = 0;
}
}
return 0;
}