Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

One of you EXPERT C PROGRAMMERS rewrite this

Name: Anonymous 2010-11-01 5:47

I'm reading K&R; doing the opposite of exercise 1-16. This really shouldn't take that much code, should it?

// Write a program to print all input lines that are shorter than 80 characters.

#include <stdio.h>
#define LINEMAX 80

int main()
{
    int c, i = 0, j, thisstr[LINEMAX];
    while ((c = getchar()) != EOF) {
        // end of a line?
        if (c == '\n') {
            // if line short enough, print the line
            if (i < LINEMAX) {
                for (j = 0; j < i; j++)
                    putchar(thisstr[j]);
                putchar('\n');
            }
            // and clear the variables for the next one
            for (i = 0; i < LINEMAX; i++)
                thisstr[i] = 0;
            i = 0;
        } else if (i < LINEMAX) {
            // store character in array
            thisstr[i] = c;
            i++;
        }
    }
    return 0;
}


Feel free to correct the shit out of me.
P.S. ((c = getchar()) != EOF) is PIG DISGUSTING

Name: Anonymous 2010-11-01 16:10

ENTERPRISE C

// Write a program to print all input lines that are shorter than 80 characters.

#include <stdio.h>
#define LINEMAX 80

int main()
{
    int c, i = 0, j, s[LINEMAX];
start:
    // end of a line?
    if (c == '\n') {
        // if line short enough, print the line
        if (i < LINEMAX) {
            j=0;
            loop1:
            putchar(s[j]);
            j++;
            if(j<i)
                goto loop1;
            putchar('\n');
        }
        // and clear the variables for the next one
        i=0;
        loop2:
        s[i] = 0;
        i++;
        if(i<LINEMAX)
            goto loop2;
        i = 0;
    } else if (i < LINEMAX) {
        // store character in array
        s[i] = c;
        i++;
    }
    if((c=getchar())!=EOF)
        goto start;
end:
    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List