Skip to content

Commit 6e4ab63

Browse files
committed
added more solutions
1 parent b0def14 commit 6e4ab63

File tree

13 files changed

+146
-0
lines changed

13 files changed

+146
-0
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

Chapter-1/Exercise 1-1/helloworld.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
printf("hello world\n");
5+
}

Chapter-1/Exercise 1-10/replace2.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
int c;
5+
while ((c = getchar()) != EOF) {
6+
if (c == '\t' || c == '\b' || c == '\\') printf("\\\\");
7+
else putchar(c);
8+
}
9+
}

Chapter-1/Exercise 1-12/word.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
int c;
5+
while ((c = getchar()) != EOF) {
6+
if (c == ' ' || c == '\t') putchar('\n');
7+
else putchar(c);
8+
}
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#define SIZE 15 // assuming the maximum size of a word is 15 letters long
3+
#define MAXHEIGHT 15 // assuming the maximum frequency of a word to be 15
4+
//else use another loop to determine the maximum height
5+
6+
main() {
7+
int c, count[SIZE] = {}, tmp = 0;
8+
while ((c = getchar()) != EOF) {
9+
if (c != ' ' && c != '\n' && c != '\t') ++tmp;
10+
else {
11+
++count[tmp-1];
12+
tmp = 0;
13+
}
14+
}
15+
for (int m = 0 ; m < MAXHEIGHT ; m++) {
16+
for (int i = 0 ; i < SIZE ; i++) {
17+
if (count[i] != 0) {
18+
if (count[i] >= MAXHEIGHT - m) putchar('#');
19+
else putchar(' ');
20+
}
21+
}
22+
putchar('\n');
23+
}
24+
25+
for (int i = 0 ; i < SIZE ; i++) {
26+
if (count[i] != 0) printf("%d", i+1);
27+
}
28+
putchar('\n');
29+
}

Chapter-1/Exercise 1-2/helloworld.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
printf("\"hello world\"\n"); // should print "hello world"
5+
//printf("\c") // throws an error since \c is not a valid escape sequence
6+
}

Chapter-1/Exercise 1-3/farhtocel.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
float fahr, celsius;
5+
int lower, upper, step;
6+
7+
lower = 0;
8+
upper = 300;
9+
step = 20;
10+
11+
fahr = lower;
12+
printf("%10s %10s", "farenheit", "celsius\n");
13+
while (fahr <= upper) {
14+
celsius = (5.0/9.0) * (fahr - 32.0);
15+
printf("%10.0f %9.2f\n", fahr, celsius);
16+
fahr += step;
17+
}
18+
}

Chapter-1/Exercise 1-4/celtofarh.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
float celsius, fahr;
5+
6+
int start = 0;
7+
int end = 300;
8+
int step = 20;
9+
10+
celsius = start;
11+
12+
printf("%10s %10s\n", "celsius", "farenheit");
13+
while (celsius <= end) {
14+
fahr = ((9.0/5.0) * (celsius)) + 32.0;
15+
printf("%10.0f %10.2f\n", celsius, fahr);
16+
celsius += step;
17+
}
18+
}

Chapter-1/Exercise 1-5/celtofarh.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
printf("%10s %10s\n", "celsius", "farenheit");
5+
for (float celsius = 300; celsius >= 0 ; celsius -= 20) {
6+
printf("%10.0f %10.2f\n", celsius, (9.0*celsius/5.0) + 32.0);
7+
}
8+
}

Chapter-1/Exercise 1-6/eof.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
3+
// ctrl + d produces the EOF character, which will print 0 since the realtion becomes false
4+
// and any other character will print 1m since the realtion is true
5+
main() {
6+
printf("%d\n", getchar() != EOF);
7+
}

0 commit comments

Comments
 (0)