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 0:09

>>21
NYJMUA

Name: Anonymous 2008-06-15 4:20

>>23
No, You Just Make Up Acronyms?

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