Commit Diff


commit - 13833da7de30c2d663df96dd16dfd1dced98117e
commit + 2b8a66882ff733f237f41c83507bb9ed4e924d75
blob - /dev/null
blob + 6b0d9f2bb23d49aa12553dbda6f06461bc5f55ae (mode 644)
--- /dev/null
+++ 3-3-input
@@ -0,0 +1,11 @@
+a-z
+b-g
+C-Q-
+a-z0-9-
+-a-z
+A-B-c-
+c-G-Q-
+d-h-v2-4e-g
+c-g-J-U2-5-7c-gi-o
+Some texT-Y2-5 then some more-u
+c-f-G-Q3-5-9
blob - /dev/null
blob + 77d079e2acde0c6c7b33433d63151fa069d26e53 (mode 644)
--- /dev/null
+++ 3-3-output
@@ -0,0 +1,11 @@
+abcdefghijklmnopqrstuvwxyz
+bcdefg
+CDEFGHIJKLMNOPQ-
+abcdefghijklmnopqrstuvwxyz0123456789-
+-abcdefghijklmnopqrstuvwxyz
+AB-c-
+c-GHIJKLMNOPQ-
+defghijklmnopqrstuv234efg
+cdefg-JKLMNOPQRSTU234567cdefgijklmno
+Some texTUVWXY2345 then some morefghijklmnopqrstu
+cdef-GHIJKLMNOPQ3456789
blob - /dev/null
blob + 42535372f7b2651fde1afe062ecd3e276c16430f (mode 644)
--- /dev/null
+++ 3-3.c
@@ -0,0 +1,87 @@
+/* 3-3 Write a function expand(s1,s2) that expands shorthand notations like
+ * a-z in the string s1 into the equivalent complete list abc...xyz in s2.
+ * Allow for letters of either case and digits, and be prepared to handle cases
+ * like a-b-c and a-z0-9 and -a-z. Arrange that a leading or trailing - is
+ * taken literally. */
+
+#include <stdio.h>
+
+#define MAXLINE 1000		/* maximum input line size */
+
+int islwer(char c);
+int isuppr(char c);
+int isdgt(char c);
+int getlin(char s[], int lim);
+int expand(const char s1[], char s2[]);
+
+int main() {
+	int len;
+	char input[MAXLINE];
+	char output[MAXLINE];
+	while ((len=getlin(input,MAXLINE))>0) {
+		len = expand(input, output);
+		printf("%s", output);
+	}
+
+	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;
+}
+
+int islwer(char c) {
+	return c >= 'a' && c <= 'z';
+}
+
+int isuppr(char c) {
+	return c >= 'A' && c <= 'Z';
+}
+
+int isdgt(char c) {
+	return c >= '0' && c <= '9';
+}
+
+/* expand(s1,s2) that expands shorthand notations like
+ * a-z in the string s1 into the equivalent complete list abc...xyz in s2.
+ * Allow for letters of either case and digits, and be prepared to handle cases
+ * like a-b-c and a-z0-9 and -a-z. Arrange that a leading or trailing - is
+ * taken literally. */
+
+/* expand: expand a-z0-9 shorthand notation from s1 into complete list in s2;
+ * return length of s2 */
+int expand(const char s1[], char s2[]) {
+	int i, j;
+	for (i = 0, j = 0; s1[i] != '\0'; i++) {
+		if (islwer(s1[i]) && s1[i+1] == '-' && islwer(s1[i+2])) {
+			for (char c = s1[i]; c < s1[i+2]; c++, j++) {
+				s2[j] = c;
+			}
+			i++;
+		} else if (isuppr(s1[i]) && s1[i+1] == '-' && isuppr(s1[i+2])) {
+			for (char c = s1[i]; c < s1[i+2]; c++, j++) {
+				s2[j] = c;
+			}
+			i++;
+		} else if (isdgt(s1[i]) && s1[i+1] == '-' && isdgt(s1[i+2])) {
+			for (char c = s1[i]; c < s1[i+2]; c++, j++) {
+				s2[j] = c;
+			}
+			i++;
+		} else {
+			s2[j++] = s1[i];
+		}
+	}
+	s2[j] = '\0';
+	return j;
+}