aboutsummaryrefslogtreecommitdiff
path: root/7.2/main.c
diff options
context:
space:
mode:
Diffstat (limited to '7.2/main.c')
-rw-r--r--7.2/main.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/7.2/main.c b/7.2/main.c
new file mode 100644
index 0000000..f11256e
--- /dev/null
+++ b/7.2/main.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <ctype.h>
+#include "getch.h"
+
+#define MAXLINE 80
+#define OCTLEN 6
+
+int inc(int col, int n);
+
+int main(void)
+{
+ char c, col, next;
+
+ col = 0;
+ while ((c = getch()) != EOF) {
+ if (iscntrl(c) || isblank(c)) {
+ col = inc(col, OCTLEN);
+ printf(" \\%03o ", c);
+
+ if (c == '\n') {
+ col = 0;
+ putchar(c);
+ }
+ } else {
+ col = inc(col, 1);
+ /* ac-
+ * k */
+ ungetch(next = getch());
+ if (col == MAXLINE && isalpha(c) &&
+ isalpha(next)) {
+ ungetch(c);
+ c = '-';
+ }
+
+ putchar(c);
+ }
+ }
+
+ return 0;
+}
+
+int inc(int col, int n)
+{
+ if (col + n > MAXLINE) {
+ putchar('\n');
+ return n;
+ } else {
+ return col + n;
+ }
+}