summaryrefslogtreecommitdiff
path: root/one
diff options
context:
space:
mode:
Diffstat (limited to 'one')
-rw-r--r--one/char.c53
-rw-r--r--one/count.c41
-rw-r--r--one/longest.c86
-rw-r--r--one/power.c21
-rw-r--r--one/tab.c59
-rw-r--r--one/temp.c31
-rw-r--r--one/uncom.c34
7 files changed, 325 insertions, 0 deletions
diff --git a/one/char.c b/one/char.c
new file mode 100644
index 0000000..91cfc75
--- /dev/null
+++ b/one/char.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+
+#define IN 1 /* inside of a word */
+#define OUT 0 /* outside of a word */
+
+/* count lines, words, and characters */
+void
+wc()
+{
+ /*
+ * c must be big enough to hold any value getchar() returns so we use
+ * int instead of char to hold EOF, an integer defined in stdio
+ */
+ int c, nl, nw, nc, state;
+ state = OUT;
+ nl = nw = nc = 0;
+ while ((c = getchar()) != EOF) {
+ ++nc;
+ if (c == '\n') {
+ ++nl;
+ }
+ /* && is higher precedence than || */
+ if (c == ' ' || c == '\t' || c == '\n') {
+ state = OUT;
+ } else if (state == OUT) {
+ state = IN;
+ ++nw;
+ }
+ }
+ printf("%d %d %d\n", nl, nw, nc);
+}
+
+/* print input separated by new lines */
+void
+print_word_line()
+{
+ int c;
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\t' || c == '\n') {
+ putchar('\n');
+ } else {
+ putchar(c);
+ }
+ }
+}
+
+int
+main()
+{
+ wc();
+ /* print_word_line(); */
+ return 0;
+}
diff --git a/one/count.c b/one/count.c
new file mode 100644
index 0000000..0f5bb82
--- /dev/null
+++ b/one/count.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+
+/* count digits, white space, and others */
+int
+main()
+{
+ int c, i, nwhite, nother;
+ int ndigit[10];
+ int nchar[26];
+ nwhite = nother = 0;
+
+ for (i = 0; i < 26; ++i) {
+ if (i < 10) {
+ ndigit[i] = 0;
+ }
+ nchar[i] = 0;
+ }
+
+ while ((c = getchar()) != EOF) {
+ if (c >= '0' && c <= '9') {
+ ++ndigit[c - '0'];
+ } else if (c == ' ' || c == '\t' || c == '\n') {
+ ++nwhite;
+ } else if (c >= 'a' && c <= 'z') {
+ ++nchar[c - 'a'];
+ } else {
+ ++nother;
+ }
+ }
+
+ printf("digits =");
+ for (i = 0; i < 10; ++i) {
+ printf(" %d", ndigit[i]);
+ }
+ printf("\nchars =");
+ for (i = 0; i < 26; ++i) {
+ printf(" %d", nchar[i]);
+ }
+ printf("\nwhite space = %d\nother = %d\n", nwhite, nother);
+ return 0;
+}
diff --git a/one/longest.c b/one/longest.c
new file mode 100644
index 0000000..cf19ac9
--- /dev/null
+++ b/one/longest.c
@@ -0,0 +1,86 @@
+#include <stdio.h>
+
+#define MAXLINE 1024
+
+/*
+ * read a line into s, return length,
+ * assume length is less than MAXLINE
+ * and the line number is less than MAXLINE
+ */
+int
+get_line(char s[], int lim)
+{
+ int c, i;
+ for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
+ s[i] = c;
+ }
+ if (c == '\n') {
+ /* increment i after assignment */
+ s[i++] = c;
+ }
+ /* put null character at end of string */
+ s[i] = '\0';
+ return i;
+}
+
+/* copy from into to */
+void
+copy(char to[], char from[])
+{
+ int i = 0;
+ /* '\0' marks end of string */
+ while ((to[i] = from[i]) != '\0') {
+ ++i;
+ }
+}
+
+/* remove trailing whitespace */
+void
+trim(char s[], int i)
+{
+ /* skip null character and new line */
+ i -= 2;
+ while (i >= 0 && (s[i] == ' ' || s[i] == '\t')) {
+ --i;
+ }
+ s[i] = '\0';
+}
+
+/* flip characters in string */
+void
+reverse(char s[])
+{
+ int i = 0;
+ while (s[i] != '\n') {
+ ++i;
+ }
+ --i;
+ char c;
+ for (int j = 0; j < i / 2; ++j) {
+ c = s[i - j];
+ s[i - j] = s[j];
+ s[j] = c;
+ }
+}
+
+/* print longest input line */
+int
+main()
+{
+ char line[MAXLINE], longest[MAXLINE];
+
+ int len;
+ int max = 0;
+ while ((len = get_line(line, MAXLINE)) > 0) {
+ if (len > max) {
+ max = len;
+ copy(longest, line);
+ }
+ }
+ if (max > 0) {
+ /* trim(longest, max); */
+ /* reverse(longest); */
+ printf("%s", longest);
+ }
+ return 0;
+}
diff --git a/one/power.c b/one/power.c
new file mode 100644
index 0000000..dd2ba2d
--- /dev/null
+++ b/one/power.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+int
+power(int m, int n)
+{
+ int p;
+ for (p = 1; n > 0; --n) {
+ p *= m;
+ }
+ return p;
+}
+
+int
+main()
+{
+ int i;
+ for (i = 0; i < 10; ++i) {
+ printf("%d %d %d\n", i, power(2, i), power(-3, i));
+ }
+ return 0;
+}
diff --git a/one/tab.c b/one/tab.c
new file mode 100644
index 0000000..f9c6dc5
--- /dev/null
+++ b/one/tab.c
@@ -0,0 +1,59 @@
+#include <stdio.h>
+
+/* XXX: dependent on file size */
+#define MAX 1024
+
+void detab()
+{
+ char detab[MAX];
+
+ int c, i;
+ i = 0;
+ while (i < MAX && (c = getchar()) != EOF) {
+ if (c == '\t') {
+ for (int t = 0; t < 8; ++t) {
+ detab[i + t] = ' ';
+ }
+ i += 8;
+ } else {
+ detab[i++] = c;
+ }
+ }
+ detab[i] = '\0';
+ printf("%s", detab);
+}
+
+void entab()
+{
+ char entab[MAX];
+
+ int c, i, s;
+ i = s = 0;
+ while (i < MAX && (c = getchar()) != EOF) {
+ if (c == ' ') {
+ ++s;
+ } else {
+ while (s > 0) {
+ if (s >= 8) {
+ entab[i++] = '\t';
+ s -= 8;
+ } else {
+ entab[i++] = ' ';
+ --s;
+ }
+ }
+ entab[i++] = c;
+ }
+ }
+
+ entab[i] = '\0';
+ printf("%s", entab);
+}
+
+int
+main()
+{
+ detab();
+ /* entab(); */
+ return 0;
+}
diff --git a/one/temp.c b/one/temp.c
new file mode 100644
index 0000000..54c99ba
--- /dev/null
+++ b/one/temp.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+
+#define LOWER 0
+#define UPPER 300
+#define STEP 20
+
+void
+temp()
+{
+ float fahr, cel;
+
+ printf("Fahrenheit-Celsius table\n");
+ for (fahr = UPPER; fahr >= LOWER; fahr -= STEP) {
+ cel = (5.0 / 9.0) * (fahr - 32.0);
+ /* 3 and 6 characters wide with 1 digit after the dot */
+ printf("%3.0f %6.1f\n", fahr, cel);
+ }
+
+ printf("\nCelsius-Fahrenheit table\n");
+ for (cel = LOWER; cel <= UPPER; cel += STEP) {
+ fahr = 9.0 / 5.0 * cel + 32.0;
+ printf("%3.0f %6.0f\n", cel, fahr);
+ }
+}
+
+int
+main()
+{
+ temp();
+ return 0;
+}
diff --git a/one/uncom.c b/one/uncom.c
new file mode 100644
index 0000000..455f138
--- /dev/null
+++ b/one/uncom.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+
+/* XXX: dependent on file size */
+#define MAX 2048
+
+int
+main()
+{
+ char uncomment[MAX];
+ int c, i;
+ i = 0;
+ while (i < MAX && (c = getchar()) != EOF) {
+ if (c == '/') {
+ c = getchar();
+ if (c == '*') {
+ while ((c = getchar()) != '/') {
+ if (c == '\n') {
+ putchar('\n');
+ } else {
+ putchar(' ');
+ }
+ }
+ } else if (c == '/') {
+ while ((c = getchar()) != '\n') {
+ putchar(' ');
+ }
+ putchar('\n');
+ }
+ } else {
+ putchar(c);
+ }
+ }
+ return 0;
+}