c-first-steps

a C playground
Index Commits Files Refs README
commit bedfbb82574f79ce361606864f54747fb37f5dcc
parent 16465b99181f70c9732f3a0d2d5ca69da50f409a
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Tue, 17 Nov 2020 22:03:58 -0300

Added ej6.c & ej7.c to /95.11/guia03/

Diffstat:
Aguia03/ej6.c | 24++++++++++++++++++++++++
Aguia03/ej7.c | 29+++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/guia03/ej6.c b/guia03/ej6.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <string.h>
+
+
+
+int main(void) {
+
+    char origen[] = "Hello world!\n";
+    size_t i, len;
+    len = strlen(origen);    
+    char destino[len];
+
+    for(i = 0; i <= len; i++) {
+        destino[i] = origen[i];
+        if(i == len)
+            destino[i + 1] = '\0';
+    }
+
+    printf("%s", origen);
+    printf("%s", destino);
+
+
+    return 0;
+}
diff --git a/guia03/ej7.c b/guia03/ej7.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <string.h>
+
+
+int main(void) {
+
+    char s1[] = "Hello world!";
+    char s2[] = "Hola mundo!";
+
+
+    int i, s1_len, s2_len, cat_len;
+    s1_len = strlen(s1);
+    s2_len = strlen(s2);
+
+    cat_len = ((s1_len + s2_len) + 1); //    One more for the '\0' byte;
+    char cat[cat_len];
+
+    for(i = 0; i <= cat_len; i++) {
+        if (i < s1_len) {
+            cat[i] = s1[i];
+        } else if (i >= s1_len) {
+            cat[i] = s2[i - s1_len];
+        } else if (i == cat_len) {
+            cat[i + 1] = '\0';
+        }
+    }
+    printf("%s\n", cat);
+    return 0;
+}