aboutsummaryrefslogtreecommitdiff
path: root/3.2.c
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-04 12:11:15 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-04 12:11:15 +0530
commitc24973af02bc33f2f5f25d37e22ca91da5de3c47 (patch)
tree460a005b6a3a9a967bd881bc2bf01e0becbe33cc /3.2.c
inital commit
Diffstat (limited to '3.2.c')
-rw-r--r--3.2.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/3.2.c b/3.2.c
new file mode 100644
index 0000000..57aef6c
--- /dev/null
+++ b/3.2.c
@@ -0,0 +1,119 @@
+#include <stdio.h>
+
+#define MAXLEN 5000
+
+char* escape(char to[], const char from[]);
+char* etr(char to[], const char from[]);
+
+int
+main(void)
+{
+ int i;
+ char input;
+ char str[MAXLEN];
+ char e[MAXLEN];
+ char r[MAXLEN];
+
+ for (i = 0; (input = getchar()) != EOF && i+1 < MAXLEN; ++i)
+ str[i] = input;
+
+ str[i] = '\0';
+
+
+ if (str[i-1] != '\n')
+ printf("\n");
+
+ printf("%s\n", escape(e, str));
+ printf("%s\n", etr(r, e));
+
+ return 0;
+}
+
+char*
+escape(char to[], const char from[])
+{
+ int i, j;
+
+ for (i = j = 0; from[i] != '\0' && j+1 < MAXLEN; ++i)
+ switch (from[i]) {
+ case '\n' :
+ to[j++] = '\\';
+ to[j++] = 'n';
+ break;
+ case '\t' :
+ to[j++] = '\\';
+ to[j++] = 't';
+ break;
+ case '\a' :
+ to[j++] = '\\';
+ to[j++] = 'a';
+ break;
+ case '\b' :
+ to[j++] = '\\';
+ to[j++] = 'b';
+ break;
+ case '\f' :
+ to[j] = '\\';
+ to[j++] = 'f';
+ break;
+ case '\r' :
+ to[j++] = '\\';
+ to[j++] = 'r';
+ break;
+ case '\v' :
+ to[j++] = '\\';
+ to[j++] = 'v';
+ break;
+ default:
+ to[j++] = from[i];
+ break;
+ }
+
+ to[i] = '\0';
+
+ return to;
+}
+
+char*
+etr(char to[], const char from[])
+{
+ int i, j;
+
+ for (i = j = 0; from[i] != '\0'; ++i) {
+ if (from[i] == '\\')
+ switch (from[++i]) {
+ case 'n' :
+ to[j++] = '\n';
+ break;
+ case 't' :
+ to[j++] = '\t';
+ break;
+ case 'a' :
+ to[j++] = '\a';
+ break;
+ case 'b' :
+ to[j++] = '\b';
+ break;
+ case 'f' :
+ to[j++] = '\f';
+ break;
+ case 'r' :
+ to[j++] = '\r';
+ break;
+ case 'v' :
+ to[j++] = '\r';
+ break;
+ default :
+ to[j++] = '\\';
+ to[j++] = from[i];
+ break;
+ }
+
+ else
+ to[j++] = from[i];
+ }
+
+ to[j] = '\0';
+
+ return to;
+}