summaryrefslogtreecommitdiffstats
path: root/3
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-03 18:36:26 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-03 18:36:26 +0800
commita7cc187e2090a2cfa866dff07e6bbf703f06e2c8 (patch)
tree7a364f382d12a8e2d6f8a3a567cdfb07ba4da2e1 /3
parentaec5d4fd212062b5972fbbc75a5d434b01fe0474 (diff)
downloadk&r-exercises-a7cc187e2090a2cfa866dff07e6bbf703f06e2c8.tar.gz
3.3
Diffstat (limited to '3')
-rw-r--r--3/3.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/3/3.c b/3/3.c
new file mode 100644
index 0000000..3921e02
--- /dev/null
+++ b/3/3.c
@@ -0,0 +1,38 @@
+#include <ctype.h>
+#include <stdio.h>
+
+#define MAXLEN 1000
+
+/* expands shorthand notation like a-z and 0-9 for ASCII charset */
+void expand(char s1[], char s2[]);
+
+int main(int argc, char *argv[]) {
+ int i;
+ char c, s1[MAXLEN], s2[MAXLEN];
+
+ for (i = 0; i < MAXLEN - 1 && (c = getchar()) != '\n' && c != EOF; i++)
+ s1[i] = c;
+ s1[i] = 0;
+
+ expand(s1, s2);
+ printf("%s\n", s2);
+
+ return 0;
+}
+
+void expand(char s1[], char s2[]) {
+ int i, j, c;
+
+ for (i = 0, j = 0; j < MAXLEN - 1 && (c = s1[i]) != 0; i++) {
+ if (c == '-' && i > 0 && isalnum(s1[i - 1]) && isalnum(s1[i + 1])) {
+ c = s1[i - 1] + 1;
+ while (c < s1[i + 1])
+ s2[j++] = c++;
+ continue;
+ }
+
+ s2[j++] = c;
+ }
+
+ s2[j] = 0;
+} \ No newline at end of file