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

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

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

+5
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

+9
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

+9
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+
}
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

+6
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

+18
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

+18
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

+8
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

+7
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+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
3+
// in stdio.h : #define EOF (-1), hence the output is -1
4+
5+
main() {
6+
printf("%d\n", EOF);
7+
}

Chapter-1/Exercise 1-8/count.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
int c;
5+
int blank, tabs, nl;
6+
blank = tabs = nl = 0;
7+
while ((c = getchar()) != EOF) {
8+
putchar(c);
9+
if (c == ' ') ++blank;
10+
else if (c == '\t') ++tabs;
11+
else if (c == '\n') ++nl;
12+
}
13+
printf("\nBlanks : %d, Tabs : %d, New Lines : %d\n", blank, tabs, nl);
14+
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
main() {
4+
int c, FOUND = 0;
5+
while ((c = getchar()) != EOF) {
6+
if (c == ' ') FOUND = 1;
7+
else if (FOUND == 1) {
8+
putchar(' ');
9+
putchar(c);
10+
FOUND = 0;
11+
}
12+
else putchar(c);
13+
}
14+
}

0 commit comments

Comments
 (0)