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 7:26

>>1 here.

>>7
Tell me what to use then. Having read as far as to page p. 22 in the K&R, it doesn't mention another way of printing ``".

>>13
I haven't read about arrays and the switch(). But I assume that the switch is dependt on the char c, which tells what statement should be executed.
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?

>>int main(int argc, char** argv)
Please explain what ``int argc, char** argv" part do.

>>15
I haven't been introduced to the string header. But I assume it has functions for string manipulation. Also shouldn't the header be written like this: #include <string.h>?

>>18
>>- You have a lot of empty space that doesn't need to be >>there. Use a tabstop of 2 or 4, and don't put the opening { >>on a separate line.

Done. Not using notepad for editing. I'm now using vim and a tabstop of 2.(though I have emacs too)

>>- It's clearer and shorter to combine declarations and >>initializations, so you can write: int blank=0, charac=0; >>rather than int blank, charac;
>>blank = 0;
>>charac = 0;

Done.

>>- Use switch as an alternative to if/else if/else if/...

Refer to my question to >>13.

>>- Don't forget the parameters to main and the return value >>(cf. >>17)

Done. Although I havn't been introduced to what parameteres main() can hold. Also is possible for main() to have a null-statement or is it absolutely necessary to use return 0; and return nothing that way, in order for it to be valid ANSI C?

>>- It's redundant to write if(foo) {...} else if(!foo) {...} >>when the condition 'foo' has no side effects. Just use >>if(foo) {...} else {...}.

Allright. How about this?

Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.

#include <stdio.h>
main(){
  double lower = 0.0, upper = 100.0, step = 1.0, abs_zero = -273.15;
  double celsius = lower, fahr, kelvin, rankine;
  int esc;   
  printf(" ------------------------------------------- \n");
  printf("| Celsius |  Fahrenheit |  Kelvin | Rankine |\n");
  printf(" ------------------------------------------- \n");
  while (celsius <= upper){
    fahr = celsius * (9.0/5.0) + 32.0;
    kelvin = celsius + -abs_zero;
    rankine = (celsius + -abs_zero) * (9.0/5.0);
    printf("| %7.0f |%12.4f | %7.3f | %7.3f |\n", celsius, fahr, kelvin, rankine);
    printf(" ------------------------------------------- \n");
    celsius = celsius + step; /*or ++celsius;*/
  }
  printf("\nPress ENTER to terminate...");
  esc = (getchar() != EOF);
  printf("%",esc);
  return 0;
}


I extented the program to included Celsius to Kelvin & Celsius to Rankine conversion.

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