1
0
mirror of https://github.com/krislamo/knrc.git synced 2024-09-19 21:00:35 +00:00

Compare commits

...

5 Commits

5 changed files with 129 additions and 18 deletions

16
07-extra-blanks.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
int main()
{
int c;
int l;
while ((c = getchar()) != EOF) {
if (c == ' ' && c == l) {
;
} else {
printf("%c", c);
l = c;
}
}
}

17
08-unambiguous.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF) {
if (c == '\t')
printf("\\t");
else if (c == '\b')
printf("\\b");
else if (c == '\\')
printf("\\\\");
else
printf("%c", c);
}
}

25
09-word-count.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#define IN 1
#define OUT 0
/* count lines, words, and characters in input */
int main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}

View File

@ -1,22 +1,10 @@
all: hello mathvars c2f input count countlines
CC = gcc
OBJS = $(patsubst %.c,bin/%,$(wildcard *.c))
hello: 01-hello-world.c
gcc -o ./bin/01-helloworld 01-hello-world.c
all: $(OBJS)
mathvars: 02-vars-and-math.c
gcc -o ./bin/02-mathvars 02-vars-and-math.c
c2f: 03-celsius-to-fahrenheit.c
gcc -o ./bin/03-c2f 03-celsius-to-fahrenheit.c
input: 04-file-copying.c
gcc -o ./bin/04-input 04-file-copying.c
count: 05-char-counting.c
gcc -o ./bin/05-count 05-char-counting.c
countlines: 06-line-count.c
gcc -o ./bin/06-line-count 06-line-count.c
$(OBJS):
$(CC) -o ./$@ $(patsubst bin/%,%.c,$@)
clean:
$(RM) ./bin/*-*
$(RM) ./bin/*

View File

@ -152,3 +152,68 @@ https://github.com/krislamo/knrc/commit/588969b09fabab1e91ff4f4b1c37e87fc23cf76b
https://github.com/krislamo/knrc/blob/588969b09fabab1e91ff4f4b1c37e87fc23cf76b/02-vars-and-math.c[(Source)]
Move some numbers around to reverse the table.
09-word-count.c
~~~~~~~~~~~~~~~~
https://github.com/krislamo/knrc/commit/d064db2b171e3da74fd5082ae29456f64caeafeb[(Diff)]
https://github.com/krislamo/knrc/blob/d064db2b171e3da74fd5082ae29456f64caeafeb/09-word-count.c[(Source)]
Textbook example of counting lines, words, and characters from input.
Exercise 1-11: test the word count program
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No input:
----
$ ./bin/09-word-count
0 0 0
$
----
Just 3 newlines:
----
$ ./bin/09-word-count
3 0 3
$
----
Just 3 tabs:
----
$ ./bin/09-word-count
0 0 3
$
----
Just 3 spaces:
----
$ ./bin/09-word-count
0 0 3
$
----
Just a single word per line:
----
$ ./bin/09-word-count
one
word
per
line
4 4 18
$
----
Three blanks before and after:
----
$ ./bin/09-word-count
three blanks before/after 0 3 31
$
----