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 5:56

Here's some enterprise level code

#include <stdio.h>

int main(void)
{
   char s[1024];
  
   while(!feof(stdin))
   {
      t = fgets(s,1024,stdin);
     
      if(t < 80)
         puts(s);
      else if(t==1024)
      {
         while(t==1024)
            t = fgets(s,1024,stdin);
         fgets(s,1024,stdin);
      }
   }
}

Name: Anonymous 2010-11-01 6:17

I'm doing the opposite of all the exercises, too.
That is, I'm not doing any of them.

Name: Anonymous 2010-11-01 6:55

>>2
Please, that's littered with magic numbers. And what is t?
This is ENTERPRISE level code:
[code]
#include <stdio.h>
#include <string.h>

#define MAXLINE 80

int main()
{
    char line[MAXLINE + 2];
    size_t len;

    memset(line, 0, sizeof line);

    while (fgets(line, MAXLINE + 1, stdin)) {
        len = strlen(line);
        if (len <= MAXLINE) {
            fputs(line, stdout);
        }
    }
    exit(0);
}

Name: Anonymous 2010-11-01 6:58


main = interact $ unlines . filter ((< 80) . length) . lines

Name: Anonymous 2010-11-01 8:18

>>5
First time Haskell actually amazes me.

Name: Anonymous 2010-11-01 8:21

>>5-6
???????????????????????????? ???????????????????????????????????????? ???????????? ???????????????????????????????????????????????? ???????????????????????????? ???????? ???????????????????????????????????????????????? ???????????????????????????????????????????????? ????????????
????????????????????????????????????? ???????????????????????????????????????????????????? ???????? ???????????? ???????????????? ???????? ???????????????????????????????????????????? ???????????????????????????????? ????????????????????????????????,
???????????????????????????????????????? ???????????????????????? ???????????????????? ???????? ???????????? ????????????????????????????????, ???? ???????????????????????????? ???????????????????????????????????????????? ???????? ????????????????????????????, ????????????????????,
????????????????-???????????????????????????? ???????????????????????? ???????????????? ???????????????? ???????????????????????? ???????????????????????????????????????? ???????????????????????????????????????? ???????????????????????????????????????????????????? ???????? ????????????????
???????? ???????????????????????????? ???????????? ???????????????????????? ???????????? ???????????????????????????????????????????????? ???????????????????????????? ???????????? ???????????? ????????????.

???????? ???????? ???? ???????????????? ???????? ???????????????????????? ????????????, ???????????????????????????? ???????? ???????????????????????? ???????????????????? ???????????? ???????????????????????????? ???????????? ???????? ???????????????????????? ????????????
???????????????????????????????????? ???????????????????? ???????? ???????????? ????????????????????????????????.

Name: Anonymous 2010-11-01 8:51

>>7
Shut up, ???????????????????? ???????????????????? ????????????????????????.

Name: Anonymous 2010-11-01 9:00

>>6
If you find that amazing, you should see what it can really do.
It smells terrible!

Name: Anonymous 2010-11-01 9:42

#include <stdio.h>
int main(void)
{
    char s[81];
    while (s[0] = s[79] = 0, scanf("%80[^\n]%*[^\n]", s) >= 0) {
        if (!s[79])
            printf("%s\n", s);
        getchar();
    }
    return 0;
}

Name: Anonymous 2010-11-01 10:24

grep { .chars < 80 }, slurp();

Name: Anonymous 2010-11-01 10:31

grep -vE .{80}

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

Name: Anonymous 2010-11-01 16:32

>>11
slurp() takes a param. Did you mean:
lines.grep: *.chars < 80?

Name: Anonymous 2010-11-01 17:44

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
        char    *s;
        size_t  len;
        int     ret = 1;

        if (argc != 2)
                goto E1;

        len = 4 + strlen(argv[1]) + 11 + 80 + 1 + 1;
        if (!(s = malloc(len))) {
                perror("malloc()");
                goto E1;
        }

        if (snprintf(s, len,    "cat %s| gr"
                                "ep -v \".."
                                ".........."
                                ".........."
                                ".........."
                                ".........."
                                ".........."
                                ".........."
                                ".........."
                                "........\"", argv[1]) < 0) {
                perror("snprintf()");
                goto E2;
        }

        if (system(s) == -1) {
                perror("system()");
                goto E2;
        }

        ret = 0;
E2:     free(s);
E1:     return ret;
}


tuwien

Name: Anonymous 2010-11-02 13:41

>>6
awk 'length($0)<80'

Name: Anonymous 2010-11-02 14:19

>>2
      else if(t==1024)
      {
         while(t==1024)
            t = fgets(s,1024,stdin);
         fgets(s,1024,stdin);
      }


This is the most idiotic thing I've read in a while. It doesn't even work! Maybe you meant

      while(t==1024)
         t = fgets(s,1024,stdin);


?

Name: Anonymous 2010-11-02 15:50

>>17
YHBT

Name: Anonymous 2010-11-02 17:30

>>18
Reverse Trolled

Name: Anonymous 2010-11-02 20:49

section .bss
    b resb 80

section .text
    global _start

puts:
    xor rax, rax
    inc rax
    mov rdi, rax
    mov rsi, b
    mov rdx, rbx
    syscall
    ret

gets:
    xor rax, rax
    mov rdi, rax
    mov rsi, b
    mov rdx, 0x50
    syscall
    cmp rax, 0
    je fin
    jl bail
    mov rbx, rax
    ret

bail:
    mov rdx, 1
    jmp exit
fin:
    mov rdx, 0
exit:
    mov rax, 60
    syscall
    hlt

_start:
    call gets
    cmp rbx, 80
    jl ok

    mov rdi, b
    mov rax, 10
    mov rcx, 0x50
    cld
    repne scasb
    cmp rcx, 0
    jle nextnl

ok:    call puts
    jmp _start

nextnl:    mov rsi, b
    mov rdi, 0
    mov rdx, 1
    xor rax, rax
    syscall
    cmp rax, 0
    je fin
    jl bail
    cmp byte [b], 0xA
    je _start
    jmp nextnl


Only really works with terminal i/o since that waits for a newline, with pipes it might just read a small bit, print it, and continue. I'd make it robust cc -S >>13.c, but i doubt anyone on /gorp/ would care.

Name: Anonymous 2010-11-02 21:21

>>18-19
LOL I TROLL U

Name: Anonymous 2010-11-03 7:47


#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    while(getline(cin, str)){
        if(str.length() < 80)
             cour << str;
    }
}


Requesting ENTERPRISE Brainfuck, J and APL. I'm lazy for that shit.

Name: Anonymous 2010-11-03 8:09

>>22
I have a boring course in the morning, I might bother to provide a brainfuck implementation.

Name: Anonymous 2010-11-03 8:29

C/C++ faggots like to work hard

LINEMAX = 80

import sys

for i in sys.stdin:
    if len(i) < LINEMAX:
        print i,

Name: Anonymous 2010-11-03 11:22

>>24
C/C++
Get out.

Name: Anonymous 2010-11-03 12:41

>>25
Thou hast been trolleth.

Name: Anonymous 2010-11-03 12:57

C/C#

Name: Anonymous 2010-11-03 13:11

>>27
Java/C#

Name: Anonymous 2010-11-03 13:25

PHP/Perl

Name: Anonymous 2010-11-03 13:32

FIOC/Boo

Name: Anonymous 2010-11-03 16:26

v
  >$                 v
  |                 :<
>0>~:52*=!|\1+:852**`|
  |!     :<
  >,      ^

Name: Anonymous 2010-11-04 15:50

while True:
    s = raw_input("> ");
    if len(s) < 80:
        print s

Name: Anonymous 2010-11-04 15:50

>>32

The semicolon is for aesthetics

Name: Anonymous 2010-11-04 16:54

import sys
filter(lambda s:len(s)<80,sys.read().split('\n'))


inb4 15-character Perl implementation

Name: Anonymous 2010-11-04 17:05

>>34
You're too late.

Name: Anonymous 2010-11-04 17:13

>>35
Right. Never mind, that should teach me about posting without reading the thread.

Name: Anonymous 2010-11-04 18:33

n/{,80<},n*

Name: Anonymous 2010-11-05 23:44

>>34
you forgot to print out the result of filter

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