summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <gitlab@53hor.net>2019-07-11 13:34:32 -0400
committerAdam Carpenter <gitlab@53hor.net>2019-07-11 13:34:32 -0400
commit25417eccea8509f54a693cbf54bbc05d43a474c1 (patch)
tree7eadff432dc468928ba3dd4ac7a5e5c1c77805f9
parent39c0cbcc70aff47e17107826aa76cb525c83b122 (diff)
downloadlearning-c-25417eccea8509f54a693cbf54bbc05d43a474c1.tar.xz
learning-c-25417eccea8509f54a693cbf54bbc05d43a474c1.zip
Updated ch2.
-rw-r--r--ch2/2-2.c46
-rw-r--r--ch2/2-3.c42
-rw-r--r--ch2/2-4.c43
-rw-r--r--ch2/typesbin16336 -> 0 bytes
-rw-r--r--ch2/types.c25
5 files changed, 131 insertions, 25 deletions
diff --git a/ch2/2-2.c b/ch2/2-2.c
new file mode 100644
index 0000000..52b0a2e
--- /dev/null
+++ b/ch2/2-2.c
@@ -0,0 +1,46 @@
+#include<stdio.h>
+
+
+#define MAX_LEN 10
+
+
+/*
+ * Reads STDIN into string s. Is allowed to use && and ||.
+ */
+void getline_with_ops(char s[]) {
+ char c;
+ int i;
+ int lim = MAX_LEN;
+
+ for (i = 0; i < lim - 1 && (c = getchar()) != '\n' && c != EOF; ++i)
+ s[i] = c;
+
+ s[i] = '\0';
+}
+
+
+/*
+ * Reads STDIN into string s. Is not allowed to use && or ||.
+ */
+void getline_without_ops(char s[]) {
+ char c;
+ int i;
+ int lim = MAX_LEN;
+
+ for (i = 0; i < lim - 1; ++i)
+ if ((c = getchar()) != '\n')
+ if (c != EOF)
+ s[i] = c;
+
+ s[i] = '\0';
+}
+
+
+int main() {
+ char s[MAX_LEN];
+
+ //getline_with_ops(s);
+ getline_without_ops(s);
+
+ printf("%s\n", s);
+}
diff --git a/ch2/2-3.c b/ch2/2-3.c
new file mode 100644
index 0000000..54ce89f
--- /dev/null
+++ b/ch2/2-3.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+
+
+unsigned int htoi(char hex[]) {
+ char c;
+ int i;
+ int length;
+ int power;
+ unsigned int result;
+
+ for (length = 0; hex[length] != '\0'; ++length)
+
+ power = 1;
+ result = 0;
+
+ for (i = length - 1; i >= 0; --i) {
+ c = hex[i];
+ printf("%c\n", c);
+
+ if (c >= 'a' && c <= 'f')
+ c = c - 'a' + 10;
+ else if (c >= 'A' && c <= 'F')
+ c = c - 'A' + 10;
+ else if (c >= '0' && c <= '9')
+ c = c - '0';
+ else if (c == 'x' || c == 'X')
+ break;
+ else
+ return 0;
+
+ result += c * power;
+ power *= 16;
+ }
+
+ return result;
+}
+
+
+int main() {
+ char hex[] = "0x589";
+ printf("%d\n", htoi(hex));
+}
diff --git a/ch2/2-4.c b/ch2/2-4.c
new file mode 100644
index 0000000..3f122bb
--- /dev/null
+++ b/ch2/2-4.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+
+
+void squeeze_char(char s[], char c) {
+ int i;
+ int j;
+
+ for (i = j = 0; s[i] != '\0'; i++)
+ if (s[i] != c)
+ s[j++] = s[i];
+
+ s[j] = '\0';
+}
+
+
+void squeeze_str(char s1[], char s2[]) {
+ int i;
+
+ for (i = 0; s2[i] != '\0'; ++i)
+ squeeze_char(s1, s2[i]);
+}
+
+
+void squeeze_all(char s1[], char s2[]) {
+ int i;
+ int j;
+ int k;
+
+ for (k = 0; s2[k] != '\0'; ++k) {
+ for (i = j = 0; s1[i] != '\0'; ++i)
+ if (s1[i] != s2[k])
+ s1[j++] = s1[i];
+
+ s1[j] = '\0';
+ }
+}
+
+
+int main() {
+ char squeezed[] = "abracadabra";
+ squeeze_all(squeezed,"abc");
+ printf("%s\n", squeezed);
+}
diff --git a/ch2/types b/ch2/types
deleted file mode 100644
index bc09907..0000000
--- a/ch2/types
+++ /dev/null
Binary files differ
diff --git a/ch2/types.c b/ch2/types.c
deleted file mode 100644
index 8ef4c91..0000000
--- a/ch2/types.c
+++ /dev/null
@@ -1,25 +0,0 @@
-#include<stdio.h>
-
-int stringlen(char s[]) {
- int i;
-
- i = 0;
- while (s[i] != '\0')
- ++i;
- return i;
-}
-
-int main() {
- //int count = stringlen("test");
- //printf("%d\n", count);
-
-
- //enum fruit {
- // APPLE,
- // PEAR,
- // BANANA,
- //};
- //printf("%d\n", PEAR);
-
-
-}