HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Learning C: K&R 1-22: 'fold'. Is this solution very unwieldy?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thisfoldunwieldylearningverysolution

Problem

This exercise gave me some trouble, but I think I have a solution here that fulfils the spec. However it's very long compared to what I'm looking at on the solutions page at clc-wiki.net.

In the course of solving the problem I loop over my buffered line three or four times. This seems like quite a lot, and the various edge cases I have to account for means my prog is over 100 lines long (including spacing).

If anyone is kind enough to have patience to read it, I'd really appreciate your thoughts. Could it be shorter? Are there any glaring redundancies?

```
#include

#define FOLD 20

int getline( char s[], int max );
int does_line_have_blanks( char line[], int len );
int locate_last_space ( char line[], int len );
int find_split( char line[], int len, int offset);
int mark_split_and_clean_trailing_spaces( char line[], int len, int spacecount );
int print_with_split( char s[], int split_pos );

int main()
{
int len, i, split_pos, offset;
char line[FOLD+1];

len = split_pos = 0;
for ( i = 0; i 0 ) {

split_pos = find_split ( line, len, offset );

offset = print_with_split( line, split_pos );

/ clean out buffer/
for ( i = 0; i 0 ) { /if we leave inspace state, split on the preceding char /
spacecount = 0;
split_pos = i -1;
}
}
}

if ( inspace == 1 ) { / and if we left in a space... /
split_pos = mark_split_and_clean_trailing_spaces( line, len, spacecount );
}

return split_pos;
}

int mark_split_and_clean_trailing_spaces( char line[], int len, int spacecount ) {

int split_pos, i;

split_pos = i = 0;

split_pos = len - spacecount; /... split where it started /
for ( i = split_pos + 1; i < len; ++i ) { /nullify trailing spaces as per instructions/
line[i] = '\0';
}

return split_pos;
}

int print_with_split( char line[], int split_pos ) {

int i, offset;

offset = 0;

for ( i = 0; i < split_pos; ++i ) {
putchar(line[i]);
}
putchar('\n');
for ( i =

Solution

Instead of reading an entire line, then finding where to break the line, I think I'd read one word, and depending on the length of the line up to that point and the length of the word, decide whether or not to write a new-line before writing that word.

That way, the main loop would come out something like this:

while (getword(buffer, max_len)!=EOF) {
    if (current_length + strlen(buffer) > max_len) {
       print("\n");
       current_length = 0;
    }
    print(buffer);
    current_length += strlen(buffer);
}

Code Snippets

while (getword(buffer, max_len)!=EOF) {
    if (current_length + strlen(buffer) > max_len) {
       print("\n");
       current_length = 0;
    }
    print(buffer);
    current_length += strlen(buffer);
}

Context

StackExchange Code Review Q#4997, answer score: 2

Revisions (0)

No revisions yet.