Skip to content

Commit 7194750

Browse files
committed
finished solutions to Chapter 2
1 parent 7298021 commit 7194750

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

Diff for: Chapter-2/Exercise 2-10/lower.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <ctype.h>
3+
#define MAXLINE 1000
4+
5+
int _getline(char line[], int maxline);
6+
void lower(char line[]);
7+
8+
int main() {
9+
int len;
10+
char line[MAXLINE];
11+
while ((len = _getline(line, MAXLINE)) > 0) {
12+
lower(line);
13+
printf("%s", line);
14+
}
15+
return 0;
16+
}
17+
18+
void lower(char line[]) {
19+
int i = 0;
20+
while (line[i] != '\0') {
21+
line[i] = (isalpha(line[i])) ? ((isupper(line[i])) ? (line[i]+32) : (line[i])) : (line[i]);
22+
i++;
23+
}
24+
}
25+
26+
int _getline(char s[], int lim) {
27+
int c, i;
28+
for (i = 0 ; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++)
29+
s[i] = c;
30+
if (c == '\n') {
31+
s[i++] = c;
32+
}
33+
s[i] = '\0';
34+
return i;
35+
}

Diff for: Chapter-2/Exercise 2-4/squeeze.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
2+
#define MAXLINE 1000
3+
4+
int _getline(char line[], int maxline);
5+
void squeeze(char string1[], char string2[]);
6+
7+
int main() {
8+
// take two lines as input
9+
int len1, len2;
10+
char line1[MAXLINE], line2[MAXLINE];
11+
while ((len1 = _getline(line1, MAXLINE)) > 0 && (len2 = _getline(line2, MAXLINE)) > 0) {
12+
squeeze(line1, line2);
13+
printf("%s", line1);
14+
}
15+
return 0;
16+
}
17+
18+
// deletes every character in s1 that matches any of the character in s2
19+
void squeeze(char s1[], char s2[]) {
20+
int i, j;
21+
for (i = j = 0 ; s1[i] != '\n' && s1[i] != '\0' ; i++) {
22+
int FOUND = 0;
23+
for (int k = 0 ; s2[k] != '\n' && s2[k] != '\0' ; k++) {
24+
if (s1[i] == s2[k]) {FOUND = 1; break;}
25+
}
26+
if (!FOUND) s1[j++] = s1[i];
27+
}
28+
s1[j++] = '\n';
29+
s1[j] = '\0';
30+
}
31+
32+
int _getline(char s[], int lim) {
33+
int c, i;
34+
for (i = 0 ; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++)
35+
s[i] = c;
36+
if (c == '\n') {
37+
s[i++] = c;
38+
}
39+
s[i] = '\0';
40+
return i;
41+
}

Diff for: Chapter-2/Exercise 2-5/any.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#define MAXLINE 1000
3+
4+
int _getline(char line[], int maxline);
5+
int any(char string1[], char string2[]);
6+
7+
int main() {
8+
// take two lines as input
9+
int len1, len2;
10+
char line1[MAXLINE], line2[MAXLINE];
11+
while ((len1 = _getline(line1, MAXLINE)) > 0 && (len2 = _getline(line2, MAXLINE)) > 0) {
12+
// we get the location of the first character in line1 that matches any of the character in line2
13+
int loc = any(line1, line2);
14+
if (loc != -1) printf("Location : %d\n", loc);
15+
else printf("character not found\n");
16+
}
17+
return 0;
18+
}
19+
20+
int any(char s1[], char s2[]) {
21+
for (int i = 0 ; s1[i] != '\n' && s1[i] != '\0'; i++) {
22+
for (int j = 0 ; s2[j] != '\n' && s2[j] != '\0'; j++) {
23+
if (s1[i] == s2[j]) return i;
24+
}
25+
}
26+
return -1;
27+
}
28+
29+
int _getline(char s[], int lim) {
30+
int c, i;
31+
for (i = 0 ; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++)
32+
s[i] = c;
33+
if (c == '\n') {
34+
s[i++] = c;
35+
}
36+
s[i] = '\0';
37+
return i;
38+
}

Diff for: Chapter-2/Exercise 2-6/setbits.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
3+
int setbits(int x, int pos, int n, int y);
4+
5+
int main() {
6+
// let x be 10101011
7+
int x = 171;
8+
// let y be 11111111
9+
int y = 255;
10+
11+
//let position be 3, and 3 bits to be copied
12+
int p = 3;
13+
int n = 3;
14+
15+
// result must be 11111101
16+
printf("%d\n", setbits(x, p, n, y));
17+
18+
return 0;
19+
}
20+
21+
setbits(x, p, n, y) {
22+
// push n bits from pth position of x to the right and clear other bits
23+
int tmp1 = (x >> (p-n+1)) & ~(~0<<n);
24+
// erase the last n bits in y
25+
int tmp2 = y & (~0 << n);
26+
// now just OR the two together
27+
int res = tmp1 | tmp2;
28+
29+
return res;
30+
}

Diff for: Chapter-2/Exercise 2-7/invert.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
3+
int invert(int x, int pos, int n);
4+
5+
int main() {
6+
// let x be 10101011
7+
int x = 171;
8+
9+
//let position be 3, and 3 bits to be inverted
10+
int p = 3;
11+
int n = 3;
12+
13+
// result must be 10100101
14+
printf("%d\n", invert(x, p, n));
15+
16+
return 0;
17+
}
18+
19+
int invert(int x, int p, int n) {
20+
int MASK = (~0<<(p+1)) | ~(~0<<(p-n+1));
21+
int tmp1 = ~(x & ~(MASK));
22+
int tmp2 = (x & (MASK)) | ~MASK ;
23+
printf("%d %d %d\n", MASK, tmp1, tmp2);
24+
int res = tmp1 & tmp2;
25+
return res;
26+
}

Diff for: Chapter-2/Exercise 2-8/rightrot.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
3+
unsigned int rightrot(unsigned int x, int n);
4+
5+
int main() {
6+
// let x be 00000000000000000000000010101011
7+
unsigned int x = 171;
8+
int n = 4;
9+
10+
// result must be 10110000000000000000000000001010
11+
printf("%u\n", rightrot(x, n));
12+
13+
return 0;
14+
}
15+
16+
unsigned int rightrot(unsigned int x, int n) {
17+
for (int i = 0 ; i < n ; i++) {
18+
if (x & 1 == 1) {
19+
//putchar('*');
20+
x = x >> 1;
21+
x = x | ~(~0U >> 1);
22+
} else {
23+
//putchar('_');
24+
x = x >> 1;
25+
}
26+
}
27+
return x;
28+
}

Diff for: Chapter-2/Exercise 2-9/bitcount.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
int bitcount(unsigned x);
4+
5+
int main() {
6+
// let x be 10101011
7+
unsigned x = 171;
8+
9+
// result must be 5
10+
printf("%d\n", bitcount(x));
11+
12+
return 0;
13+
}
14+
15+
int bitcount(unsigned x) {
16+
int b = 0;
17+
while (x) {
18+
x &= (x-1);
19+
b++;
20+
}
21+
return b;
22+
}

0 commit comments

Comments
 (0)