Commit Diff


commit - 32aa96c343d4f605621e53ee51b976d1805428d8
commit + 0663da4b1a29f202f872b87d38aa34b052164ace
blob - /dev/null
blob + 2886f337010a6658f1246bf5249f190ff441dffb (mode 644)
--- /dev/null
+++ 1-18-output
@@ -0,0 +1,18 @@
+INPUT: 
+
+If you would       		     
+
+    	   		   
+
+   have 	a faithful Servant,		  			 
+and    		one that you like, serve		
+
+    
+yourself.	
+
+OUTPUT: 
+
+If you would
+   have 	a faithful Servant,
+and    		one that you like, serve
+yourself.
blob - /dev/null
blob + 807e73868d9ef294eaafcd37e6aab826fe38e763 (mode 644)
--- /dev/null
+++ 1-18-text
@@ -0,0 +1,9 @@
+If you would       		     
+
+    	   		   
+
+   have 	a faithful Servant,		  			 
+and    		one that you like, serve		
+
+    
+yourself.	
blob - /dev/null
blob + 5ffbcd85e9db6d0117af10a2efacebcca73bd86a (mode 644)
--- /dev/null
+++ 1-18.c
@@ -0,0 +1,44 @@
+/* 1-18 Write a program to remove trailing blanks and tabs from each line of
+ * input, and to delete entirely blank lines. */
+
+#include <stdio.h>
+
+#define MAXLINE 1000		/* maximum input line size */
+
+int getlin(char line[], int maxline);
+
+int main() {
+	int i;
+	int len;				/* current line length */
+	char line[MAXLINE];		/* current input line */
+
+	while ((len = getlin(line, MAXLINE)) > 0) {
+		/* last char in line is \0, second to last maybe \n */
+		i = len - 1;
+		if (line[i] == '\n')
+			i--;
+		while (i >= 0 && (line[i] == ' ' || line[i] == '\t'))
+			i--;
+		if (i > 0) {
+			line[++i] = '\n';
+			line[++i] = '\0';
+			for (int j = 0; line[j] != '\0'; j++)
+				putchar(line[j]);
+		}
+	}
+	return 0;
+}
+
+/* getlin: read a line into s, return length */
+int getlin(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') {
+		s[i] = c;
+		++i;
+	}
+	s[i] = '\0';
+	return i;
+}