Skip to content

Commit 0e00c67

Browse files
added practice/2023.08-k&r
1 parent 28de657 commit 0e00c67

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Hello G++.
9797
- [2023.02-hwitch](practice/2023.02-hwitch)
9898
A new technique for switch() that reduces emissions of semicolons and curly braces.
9999

100+
- [2023.08-k&r](practice/2023.08-k&r)
101+
Exercises from Kernighan & Ritchie's "The C Programming Language (2nd edition)"
102+
100103
### License
101104

102105
Creative Commons Attribution-ShareAlike 3.0 Unported ([CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/))

practice/2023.08-k&r/prog.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// note: -Wno-implicit option is required to compile with Clang
2+
3+
#ifndef IDX
4+
#define IDX 109
5+
#endif
6+
#if IDX==109
7+
// Exercise 1-9: Write a program to copy its input to its output, replacing each
8+
// string of one or more blanks by a single blank.
9+
10+
// 59 bytes (ASCII, does the same for zeros)
11+
x;main(c){for(;c>=0;c-x|x&~32&&putchar(x))x=c,c=getchar();}
12+
13+
#elif IDX==110
14+
// Exercise 1-10: Write a program to copy its input to its output, replacing each
15+
// tab by \t, each backspace by \b, and each backslash by \\.
16+
17+
// 91 bytes (also a zero is preceded by a backslash)
18+
main(c){char*p;for(;p=strchr("\t\b\\\0tb\\",c=getchar()),c>=0;printf("\\%c"+!p,p?4[p]:c));}
19+
20+
// 85 bytes (EOF = -1, little endian, unaligned read, sizeof(int) == 4)
21+
//*p;main(c){for(;p=strchr("\t\b\\\0tb\\",c=getchar()),~c;printf("\\%c"+!p,p?p[1]:c));}
22+
23+
#elif IDX==112
24+
// Exercise 1-12: Write a program that prints its input one word per line.
25+
26+
// 79 bytes (ASCII, zero is also considered a whitespace)
27+
x;main(c){for(;x+=x+!strchr(" \t\n",c=getchar()),c>=0;x&3&&putchar(x&1?c:10));}
28+
29+
#elif IDX==113
30+
// Exercise 1-13: Write a program to print a histogram of
31+
// the lengths of words in its input.
32+
33+
// 167 bytes (ASCII, zero is also considered a whitespace)
34+
l,h[99],d;main(c){for(;c>=0;)l+=strchr(" \t\n",c=getchar())||c<0?d=d>++h[l]?d:h[l],-l:l<99;
35+
for(;c<0?++l<99?h[l]?c=h[l]*75/d,printf("%3d ",l):1:0:putchar(c--?61:10););}
36+
37+
#elif IDX==114
38+
// Exercise 1-14: Write a program to print a histogram of
39+
// the frequencies of different characters in its input.
40+
41+
// 146 bytes (ASCII)
42+
l=32,h[127],d;main(c){for(;c=getchar(),c>=0;)d=c>126||d>++h[c]?d:h[c];
43+
for(;c<0?++l<127?h[l]?c=h[l]*77/d,printf("%c ",l):1:0:putchar(c--?61:10););}
44+
45+
#elif IDX==117
46+
// Exercise 1-17: Write a program to print all input lines that are longer than 80 characters.
47+
48+
// 92 bytes (ASCII)
49+
char b[81];n;main(c){for(;c=getchar(),c>=0;n*=c!=10)n<80?b[n++]=c:(*b=!printf("%s%c",b,c));}
50+
51+
#elif IDX==118
52+
// Exercise 1-18: Write a program to remove trailing blanks and tabs
53+
// from each line of input, and to delete entirely blank lines.
54+
55+
// 126 bytes (ASCII)
56+
char b[999];n,x;main(c){for(;c=getchar(),c>=0;x*=c!=10)
57+
c-32&&c-9?n=x++|c-10&&!printf("%.*s%c",c-10?n:0,b,c):n<999?b[n++]=c:0;}
58+
59+
#elif IDX==119
60+
// Exercise 1-19: Write a function reverse(s) that reverses the character string s.
61+
// Use it to write a program that reverses its input a line at a time.
62+
63+
// 67+100 bytes (ASCII)
64+
reverse(char*s){char*e=s+strlen(s),t;for(;--e>s;*e=t)t=*s,*s++=*e;}
65+
char b[999];n;main(c){for(;c>=0;c<0|c==10?n=b[n]=0,reverse(b),puts(b):n<998?b[n++]=c:0)c=getchar();}
66+
67+
#elif IDX==123
68+
// Exercise 1-23: Write a program to remove all comments from a C program.
69+
// Don't forget to handle quoted strings and character constants properly.
70+
// C comments do not nest.
71+
72+
// 172 bytes (ASCII, replaces comments with spaces)
73+
m,p=-1;main(c){for(;c>=0;p=c)c=getchar()|(p==92)<<8,m=m|p-47||p-c&&c-42?c-34&&c-39||m%c?m:m^c:c,
74+
p>=0&&putchar(m>39&&4<p%256u-9?32:p),m=42-m|p-m|c-47?m-47|c-10?m:0:!(c=32);}
75+
76+
#endif
77+

0 commit comments

Comments
 (0)