mirror of
https://github.com/krislamo/knrc.git
synced 2025-02-21 11:35:52 +00:00
Compare commits
5 Commits
93acdbe7e9
...
0c2d0fae2b
Author | SHA1 | Date | |
---|---|---|---|
0c2d0fae2b | |||
d064db2b17 | |||
c24a724efb | |||
d8a70f8b58 | |||
a8ca4de90f |
16
07-extra-blanks.c
Normal file
16
07-extra-blanks.c
Normal 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
17
08-unambiguous.c
Normal 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
25
09-word-count.c
Normal 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);
|
||||||
|
}
|
24
Makefile
24
Makefile
@ -1,22 +1,10 @@
|
|||||||
all: hello mathvars c2f input count countlines
|
CC = gcc
|
||||||
|
OBJS = $(patsubst %.c,bin/%,$(wildcard *.c))
|
||||||
|
|
||||||
hello: 01-hello-world.c
|
all: $(OBJS)
|
||||||
gcc -o ./bin/01-helloworld 01-hello-world.c
|
|
||||||
|
|
||||||
mathvars: 02-vars-and-math.c
|
$(OBJS):
|
||||||
gcc -o ./bin/02-mathvars 02-vars-and-math.c
|
$(CC) -o ./$@ $(patsubst bin/%,%.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
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) ./bin/*-*
|
$(RM) ./bin/*
|
||||||
|
65
README.adoc
65
README.adoc
@ -152,3 +152,68 @@ https://github.com/krislamo/knrc/commit/588969b09fabab1e91ff4f4b1c37e87fc23cf76b
|
|||||||
https://github.com/krislamo/knrc/blob/588969b09fabab1e91ff4f4b1c37e87fc23cf76b/02-vars-and-math.c[(Source)]
|
https://github.com/krislamo/knrc/blob/588969b09fabab1e91ff4f4b1c37e87fc23cf76b/02-vars-and-math.c[(Source)]
|
||||||
|
|
||||||
Move some numbers around to reverse the table.
|
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
|
||||||
|
$
|
||||||
|
----
|
||||||
|
Loading…
x
Reference in New Issue
Block a user