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

K&R

Name: Anonymous 2008-06-14 19:20

K&R

Exercise 1-8. Write a program to count blanks, tabs, and newlines.

#include <stdio.h>

main()
{
    int blank, charac, tab, newline;
   
    blank = 0;
    tab = 0;
    newline = 0;
   
    printf("          | Blanks | Tabs | Newlines | \n");

    while((charac = getchar()) != EOF)
    {
        if(charac == ' ')
            {       
                ++blank;   
            }
        if(charac == '\t')
            {       
                ++tab;       
            }
        if(charac == '\n')
            {       
                ++newline;
                printf("| No. of: | %6d | %4d | %8d | \n", blank, tab, newline);
            }
    }
}


Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

#include <stdio.h>

main()
{
    int input_s, space, space_c;
   
    space = ' ';
    space_c = 0;
   
    while((input_s = getchar()) != EOF)
        {   
            if(input_s != space)
                {
                putchar(input_s);
                space_c = 0;
                }
            else if(input_s == space)
                {
                ++space_c;
                if(space_c == 1)
                    {
                    putchar(input_s);
                    }
                else if(space_c > 1)
                    {
                    --space_c;
                    printf("");
                    }
                }
        }
}


Using Cygwin --GCC 3.4.4, compile with following; -ansi -m32 -pedantic.

Comments on how to make it better, what is bad and what is good, please. (1.8 sucks I know.)

Name: Anonymous 2008-06-15 14:06

>>35
Besides that, isn't the switch/case only used for testing if the program gives the expected values, and isn't used for the final program?

You are confusing it with assert(), which is not present in standard C.

>>36
I think >>17 meant that main() is wrong and int main() is right, so:

>>1
Using Cygwin --GCC 3.4.4, compile with following; -ansi -m32 -pedantic.

Forgot your -Wall

Name: Anonymous 2008-06-15 14:53

1>> here.

41>>
Oh, allright, thanks. But is it possible for example to set main() to long main() or double main() or float main() or char main() or one of the other types?

Name: Anonymous 2008-06-15 15:26

>>42
What were you trying to do?

Name: Anonymous 2008-06-15 15:39

>42<
Ask your compiler

Name: Anonymous 2008-06-15 15:40

>>43

Nothing. I just want to know if it's possible.

Name: Anonymous 2008-06-15 16:25

>>38
FUCKING TYPE int main(void) YOU FUCKING MORON.

Name: Anonymous 2008-06-15 16:33

>>41
You are confusing it with assert(), which is not present in standard C.
THAT'S FUCKING BULLSIHT.
assert() is ANSI C89, ISO C90, ISO C99. You FAILED YOU COCKSUCKER.
(there is a subtle difference between C89/90's assert and C99's assert)
>>42
IT'S FUCKING >> NOT << YOU FUCKING MORON COCKSUCKER MOTHERFUCKER
NO main() CANNOT RETURN ANYTHING BUT int. MORON.
>>45
YOURE A MORON, NO ITS NOT

Name: Anonymous 2008-06-15 17:06

>>47
I lol'd but i shouldn't.

Name: Anonymous 2008-06-15 17:34

>>1 here.

>>46,47
Your RAGE made laugh. But I understand why you RAGE over my foolishness and obviously not-smart/shit programming. And I have no excuse for doing so. Beginners fail. Although I want to learn more programming and become better at it. Of course, not only C, but other programming languages as well.

I have to admit, I RAGE too. But I believe that RAGE only attracts misery, trolls and destruction. Ultimately the result is that you're TROLLED, UTTERLY FUCKED or ``HAX MY ANUS'd". Calm down and think about it, before you RAGE even more.

Good evening, sir.

Name: Anonymous 2008-06-15 17:51


#include <stdio.h>
//Been 5 years since I last wrote anything in c
main()
{
  int counters[128];
  int index = -1;

  memset(counters,0,100);
  while((index = getchar()) != EOF)
  {
    counters[index]++;
  }

  printf("blanks:%i tabs:%i newlines:%i\n", counters[32], counters[9], counters[10]);

  return 0;
}

Name: Anonymous 2008-06-15 18:29

>>50
Not too bad, but...

int counters[128];
Not 8-bit clean.

memset(counters,0,100);
Why 28 less? Also you forgot to multiply by sizeof(int).

printf("blanks:%i tabs:%i newlines:%i\n", counters[32], counters[9], counters[10]);
Hardcoding ASCII values considered harmful.

And of course the complete lack of error checking, but that applies to everything in this thread.

>>16 fails because char c isn't large enough to hold all character values and EOF.

>>35 printf("%",esc); wat. Also the two lines before it.

Name: Anonymous 2008-06-15 19:10

main =
   interact $ unwords . zipWith (++) ["blanks: ","tabs: ","newlines: "] . counts
   where counts s = map (\c -> show $ length $ filter (==c) s) [' ','\t','\n']

Name: Anonymous 2008-06-15 19:25

>>52
Too  bad  that  it's  slow  as  fuck.

Name: Anonymous 2008-06-15 19:31

>>53

2/10

Name: Anonymous 2008-06-15 20:10

Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
main = interact $ unwords . words

Name: Anonymous 2008-06-15 20:11

>>53
You do know that Haskell compilers can make certain assumptions that would be fatal if a C compiler also assumed the same things? C programs are unstable as fuck.

Name: >>55 2008-06-15 20:17

main = interact $ unlines . map (unwords . words) . lines

Name: Anonymous 2008-06-15 20:19

>>54,56
If such small Haskell programmes are so fast, then why isn't it so popular?

Name: Anonymous 2008-06-15 20:28

>>58
because of the smell.

Name: Anonymous 2008-06-15 20:28

>>58
Monads.

Name: Anonymous 2008-06-15 20:42

>>58
Uh, because that's a trivial program.

Name: Anonymous 2008-06-15 21:52

>>58
Because you have to be a mathematician to make sense of it.

Name: Anonymous 2008-06-15 22:03

>>62

5/10

Name: Anonymous 2008-06-15 22:41

>>49
LEARN TO TROLL YOU FUCKING PIECE OF SHIT YOU THINK I DON'T KNOW YOU DID THAT SHIT << ON PURPOSE? DO YOU REALLY THINK TROLLING IS THAT EASY? YOU FUCKING SCATCUMHOLE, YOU CORNY FAGGOTASSLICKER GODCOCKSHITFUCKMOTHERDAD GOD DAMMITFUCK.
NOW GO RIDE THE FUCKING TAJ MAHAL OF DICKS.
>>50
memset(counters,0,100);
YOU HAVE TO MULTIPLY THE NUMBER OF ELEMENTS WITH THE SIZE OF EACH ELEMENT OR INITIALIZE THE CORNYFAGARRAY TO {0} GOD DAMN FUCK
BOB SCATGET YOUR FUCKING GCODE HAS SO MANY ERRORS ILL POST MY OWN YOU FAGGOTASSCOCKSUCKERLICKERMOTHERFUCKE


#include <stdio.h>
#include <limits.h>
int main(void) {
  unsigned array[2 << CHAR_BIT];
  int c;
  while(((c = getchar()) != EOF) array[c]++;
  /* printf array['\n'] array[' '] array['\t'] */
  return 0;
}

TEHRE YOU GO GODFUCKDAMN STAY AWAY FROM C YOU FUCKING MORONSHITFUCK.

>>51
Not 8-bit clean.
GTFO YOU FUCKING NONPORTABLE FAGGOT. YOU ARE NOT CHAR_BIT CLEAN.
Why 28 less? Also you forgot to multiply by sizeof(int).
IT'S NOT 28 LESS. I HAVE NO FUCKING IDEA WHAT THE FUCK YOU'RE TALKING ABOUT AND YOU PROBABLY DO NOT AS WELL.
Hardcoding ASCII values considered harmful.
INDEED.
>>16 fails because char c isn't large enough to hold all character values and EOF.
WELL HERE'S A UFCKING SURPRISE TO YOU int IS NOT BIG ENOUGH FOR ALL unsigned char VALUES AND EOF EITHER. YOU NEED TO USE feof() AND ferror(). COCKLIPS

>>56
OH GOD DONT FUCKING START ME ON THIS LEARN A BIT ABOUT COMPILER THEORY AND OPTIMIZATION FIRST YOU DICKROCKER
>>60
MONADS ARE FUCKING TRIVIAL
>>62
WHAT SHUT UP
 SHUTT UPPPPPP SUHT YOUR WHORE MOUTH YOU FUCKING FUCK
>>63
GTFO WITH YOUR GOD DAMN RANKS NOBODY GIVES A SHIT
GOD DAMNGODF UCK SAGE

Name: Anonymous 2008-06-15 22:43

>>64
OH GOD MOTHERSHITFUCK THERES A MISTAKE GOD DAMN FUCK
unsigned array[2 << CHAR_BIT];
CHANGE THAT SHIT TO
unsigned array[1 << (CHAR_BIT-1)];
FUCKMOTHERS ALL OF THEM

Name: Anonymous 2008-06-15 22:48

>>63
You can't rate the truth.

Name: Anonymous 2008-06-15 22:49

>>47,64,65
Shut up Fullforce.

Name: Anonymous 2008-06-15 22:54

>>38
Your indentation is still horrible.

This is superior:
#include <stdio.h>
main(){
  int input_s, space = ' ', space_c = 0;
  while((input_s = getchar()) != EOF) {
    if(input_s != space) {
      putchar(input_s);
      space_c = 0;
    } else {
      ++space_c;
      if(space_c == 1)
       putchar(input_s);
      else
       --space_c;
    }
  }
  return 0;
}


I also notice the abundance of trailing whitespace. Use a instead of i to minimize having to remove it afterwards.

Name: Anonymous 2008-06-15 22:55

>>67
FUCK YOU FAGGOT IM NOT THAT FAGGOTFUCK I NEVER POST WITH TRIPCODES OR NAMES. ALWAYS SAGE MODE. ALWAYS RAGE MODE. FUCK YOU FUCKINGSHITFUCK

Name: Anonymous 2008-06-15 22:57

>>68
THAT DOESN'T WORK YOU MORON

Name: Anonymous 2008-06-15 23:12

>>62
What's that? You need math skills in order to understand the applications of information and computation theory? Terrible!

Name: Anonymous 2008-06-15 23:28

You want bad references?
You want horrible humor execution?
>>71 is PIG DISGUSTING.

Name: Anonymous 2008-06-15 23:56

>>71
Did I say "math skills", or did I say "mathematician"? Most languages don't involve so much "monad monad monad functor (arrows)". And all the nasty syntax keeps us Lispers off too.

Name: Anonymous 2008-06-16 0:00

>>51
And of course the complete lack of error checking, but that applies to everything in this thread.

There's no need to error check in this program. And getchar even returns EOF on error. Are you suggesting to error-check printf?

Name: Anonymous 2008-06-16 0:33

Are you suggesting to error-check printf?

yibbit.

Name: Anonymous 2008-06-16 1:06

>>1 here.

>>64,65,69
Actully, I'm not trolling.

You're the CAPS-LOCK cancer that is killing /prog/ and this thread.I recommend seeking a professional psychiatrist and/or anti-rage group psychotherapy.

Good day, sir.

>>68
int main() so it doesn't throw up a warning when compiling.

>>70
It does work.

Name: Anonymous 2008-06-16 1:20

>>76
no it doesn't you fucking faggot

Name: Anonymous 2008-06-16 3:42

>>77
Explain yourself

Name: Anonymous 2008-06-16 4:03

>>78
5.1.2.2.1

Name: Anonymous 2008-06-16 5:44

>>76
You have issues.

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