diff options
-rw-r--r-- | copier.c | 29 | ||||
-rw-r--r-- | counter.c | 29 | ||||
-rw-r--r-- | ctof.c | 24 | ||||
-rw-r--r-- | ftoc.c | 32 | ||||
-rw-r--r-- | hello.c | 8 | ||||
-rw-r--r-- | line_counter.c | 16 | ||||
-rw-r--r-- | whitespace_counter.c | 22 |
7 files changed, 160 insertions, 0 deletions
diff --git a/copier.c b/copier.c new file mode 100644 index 0000000..eeade8e --- /dev/null +++ b/copier.c @@ -0,0 +1,29 @@ +#include <stdio.h> + +/* Copy input to output; 1st version */ +//main() { +// int c; +// +// c = getchar(); +// +// while (c != EOF) { +// putchar(c); +// c = getchar(); +// } +//} + +/* Copy input to output; 2nd version */ +main() { + int c; + + while ((c = getchar()) != EOF) { + putchar(c); + } +} + +//main() { +// printf("%d", EOF); +// getchar(); +// printf("%d", getchar()); +// printf("%d", getchar()); +//} diff --git a/counter.c b/counter.c new file mode 100644 index 0000000..a1b9927 --- /dev/null +++ b/counter.c @@ -0,0 +1,29 @@ +#include<stdio.h> + +/* + * Count characters in input; 1st version + */ +//main() { +// long nc; +// +// nc = 0; +// +// while (getchar() != EOF) +// ++nc; +// +// +// printf("%ld\n", nc); +//} + +/* + * Count characters in input; 2nd version + */ +main() { + long nc; + + + for (nc = 0; getchar() != EOF; ++nc) + ; + + printf("%ld\n", nc); +} @@ -0,0 +1,24 @@ +#include<stdio.h> + +/* + * Print Celcius-Fahrenheit table for celcius = 0, 20, ... 300. + */ +main() { + float fahr, celcius; + int lower, upper, step; + + lower = 0; // lower limit of temperature table + upper = 300; // upper limit of temperature table + step = 20; // step size + + celcius = lower; + + printf("%3s %6s\n", "C", "F"); + + while (celcius <= upper) { + fahr = (9.0 / 5.0) * celcius + 32.0; + printf("%3.0f %6.1f\n", celcius, fahr); + celcius = celcius + step; + } +} + @@ -0,0 +1,32 @@ +#include<stdio.h> + +#define LOWER 0 // lower limit of table +#define UPPER 300 // upper limit of table +#define STEP 20 // step size + +/* + * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300. + */ +main() { + float fahr, celcius; + int lower, upper, step; + + lower = 0; // lower limit of temperature table + upper = 300; // upper limit of temperature table + step = 20; // step size + + fahr = lower; + + printf("%3s %6s\n", "F", "C"); + + // while (fahr <= upper) { + // celcius = 5.0 / 9.0 * (fahr - 32.0); + // printf("%3.0f %6.1f\n", fahr, celcius); + // fahr = fahr + step; + // } + + for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) { + printf("%3.0f %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32)); + } +} + @@ -0,0 +1,8 @@ +#include<stdio.h> + +main() { + printf("hello, "); + printf("world."); + printf("\n"); +} + diff --git a/line_counter.c b/line_counter.c new file mode 100644 index 0000000..0447aa7 --- /dev/null +++ b/line_counter.c @@ -0,0 +1,16 @@ +#include<stdio.h> + +/* + * Count lines in input + */ +main() { + int c, nl; + + nl = 0; + + while ((c = getchar()) != EOF) + if (c == '\n') + ++nl; + + printf("%d\n", nl); +} diff --git a/whitespace_counter.c b/whitespace_counter.c new file mode 100644 index 0000000..ea441e2 --- /dev/null +++ b/whitespace_counter.c @@ -0,0 +1,22 @@ +#include<stdio.h> + +/* + * Count whitespace in input + */ +main() { + int c, n_whitespace; + + n_whitespace = 0; + + while ((c = getchar()) != EOF) { + if (c == '\n') + ++n_whitespace; + if (c == '\t') + ++n_whitespace; + if (c == ' ') + ++n_whitespace; + } + + + printf("%d\n", n_whitespace); +} |