k&r ex1-22

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;
}
 
M

Michael Mair

aegis said:
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?

Nope. I will write '*' instead of ' ' for clarity:
"beautiful*is*a*nine*letter*word"

1234567890
beautiful*is*a*nine*letter*word\0
becomes
beautiful\nis*a*nine*letter*word\0
Continue after \n:
1234567890
is*a*nine*letter*word\0
becomes
is*a*nine\nletter*word\0
Continue after \n:
1234567890
letter*word\0
becomes
letter\nword\0

Leading to

beautiful\n
is*a*nine\n
letter\n
word

Take 9 instead of 10 and you will have the same result (the \n
is not "visible"). Take 8 and you will have to do something
about beautiful:
12345678
beautifu\n
l*is*a\n
nine\n
letter\n
word\0

or

12345678
beautif-\n
ul*is*a\n
nine\n
letter\n
word\0

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') {
Careful: You probably mean
if ( i == (COLUMN - 1) && (c == ' ' || c == '\t') )
&& has higher precedence than || (that is, your code
is equivalent to
if ( (i == (COLUMN - 1) && c == ' ') || c == '\t' )
which probably is not your intention
++i;
++bt;
} else if(i == COLUMN) {
putchar('\n');
i = 0;
}
if(i < COLUMN && !bt) {
putchar(c);
++i;
} else if(bt) {
bt = 0;
}
You do not need the last: just set bt to zero every
time.
}

return 0;
}

If you use getchar(), then you either have to store the stuff
in a buffer or implement it the way you have been doing (but
for discarding unnecessary "visible" whitespaces).

BTW: I would give a meaningful name to 'bt' -- as it is no
loop counter but flags meeting a certain condition a name
indicating its purpose helps when revisiting the code after
some time...
If you do not restrict yourself to elementary stuff, you can
also have a look at a recent thread
groups.google.com/groups?selm=316ev7F384kvbU1%40individual.net
where I gave a function solving a slightly more complicated
problem.


Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top