c-first-steps

a C playground
Index Commits Files Refs README
commit 38474909a99d29efb03002231a495578a2b7a34c
parent d1cbe5d9bc539cc44a88e186f92533eed2e2245a
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Thu,  3 Dec 2020 20:54:51 -0300

Added more exercices (mostly unfinished)

Diffstat:
A95.11/guia03/a.out | 0
M95.11/guia03/ex26.c | 23++++++++++-------------
A95.11/guia03/ex27.c | 27+++++++++++++++++++++++++++
A95.11/guia03/ex28.c | 13+++++++++++++
A95.11/guia03/ex29.c | 16++++++++++++++++
A95.11/guia05/ex02.c | 0
A95.11/guia06/a.out | 0
A95.11/guia06/ex01.c | 20++++++++++++++++++++
A95.11/guia07/ex05.c | 31+++++++++++++++++++++++++++++++
9 files changed, 117 insertions(+), 13 deletions(-)
diff --git a/95.11/guia03/a.out b/95.11/guia03/a.out
Binary files differ.
diff --git a/95.11/guia03/ex26.c b/95.11/guia03/ex26.c
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 
 #define MAX_LEN 100
-
+#define ERR_MAX_STR_LEN "Err: the entered number is too big"
 
 
 int main ( void ) {
@@ -16,12 +16,11 @@ int main ( void ) {
     }
 
 //  This part couts the lengh of the first string
-//    and removes the new line character;
+//    and replaces the new line character for a blank space;
     for(i = 0; str1[i] != '\0'; i++)
         if(str1[i] == '\n')
-            str1[i] = str1[i + 1];
-
-    printf("str1 length = %d\n", i);
+            str1[i] = ' ';
+            str1[i + 1] = '\0';
 
     if(fgets(str2, MAX_LEN, stdin) == NULL) {
         return 1;
@@ -32,30 +31,28 @@ int main ( void ) {
     }
 
     num = atoi(buffer);
-    printf("str1 = %s\n", str1);
-    printf("str2 = %s\n", str2);
-    printf("num = %d\n", num);
 
 //  This part checks if the input number is bigger than
-//  the length of the string;
+//  the length of the first string;
     if(num > i) {
+        fprintf(stderr, ERR_MAX_STR_LEN"\n");
         return 1;
     }
 
+
+//    Removes the new line chracter from string two;
     for(i = 0; str2[i] != '\0'; i++)
         if(str2[i] == '\n')
             str2[i] = '\0';
 
     for(i = 0, j = 0; str2[j] != '\0'; i++) {
-        if(i <= num) {
+        if((i <= num) && (str1[i] != '\0')) {
             dest[i] = str1[i];
         } else { 
             dest[i] = str2[j];
             j++;
         }
     }
-//    printf("%d\n", i);
-//    printf("%d\n", j);
-//    printf("%s\n", dest);
+    printf("%s\n", dest);
     return 0;
 }
diff --git a/95.11/guia03/ex27.c b/95.11/guia03/ex27.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_LEN 100
+
+int main ( void ) {
+
+    char str[MAX_LEN], buffer[MAX_LEN];
+    int i, j, num;
+
+    if(fgets(str, MAX_LEN, stdin) == NULL)
+        return 1;
+
+    if(fgets(buffer, MAX_LEN, stdin) == NULL)
+        return 1;
+
+    num = atoi(buffer);
+
+    for(i = 0; str[i] != '\0'; i++)
+        if(str[i] == '\n')
+            i++;
+            for(j = 0; j <= num; j++)
+                str[i + j] = ' ';
+
+    printf("%s\n", str);
+    return 0;
+}
diff --git a/95.11/guia03/ex28.c b/95.11/guia03/ex28.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+#define MAX_LEN 20
+
+int main (void) {
+
+    int i, vector[MAX_LEN];
+
+    for(i = 0; i < MAX_LEN; i++)
+        printf("%d\n", vector[i]);
+
+    return 0;
+}
diff --git a/95.11/guia03/ex29.c b/95.11/guia03/ex29.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+#define MAX_LEN 20
+
+int main (void) {
+
+    int i, num, vector[MAX_LEN];
+    char buffer[MAX_LEN];
+
+    
+
+    for(i = 0; i < MAX_LEN; i++)
+        printf("%d\n", vector[i]);
+
+    return 0;
+}
diff --git a/95.11/guia05/ex02.c b/95.11/guia05/ex02.c
diff --git a/95.11/guia06/a.out b/95.11/guia06/a.out
Binary files differ.
diff --git a/95.11/guia06/ex01.c b/95.11/guia06/ex01.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ERR_ARG "Error: No arguments"
+
+int main (int argc, char *argv[]) {
+    size_t i;
+    if(argc == 1) {
+        fprintf(stderr, ERR_ARG"\n");
+        return 1;
+    }
+
+    printf("argc: %d \n", argc);
+
+    for (i = 0; i < argc; i++) {
+        printf("argv[%ld] = %s \n", i, argv[i]);
+    }
+
+    return EXIT_SUCCESS;
+}
diff --git a/95.11/guia07/ex05.c b/95.11/guia07/ex05.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MASK_BYTE 0xFF
+
+int main(void) {
+
+//    0x10 30 25 F4
+    unsigned int n = 271590900;
+
+//    Arreglo de 4 bytes;
+    unsigned char v[4]; 
+
+    size_t i;
+/*
+    v[0] = (n >> 24) & 0xFF;
+    v[1] = (n >> 16) & 0xFF;
+    v[2] = (n >> 8) & 0xFF;
+    v[3] = (n >> 0) & 0xFF;
+*/
+    for(i = 0; i < sizeof(unsigned int); i++)
+        v[i] = (n >> ( (sizeof(unsigned int) - 1 - i) * 8  )) & 0xFF;
+
+    printf("%d\n", n);
+    printf("%x\n", n);
+
+    for(i = 0; i < sizeof(unsigned int); i++)
+        printf("v[%lu] = %x\n", i, v[i]);
+
+    return EXIT_SUCCESS;
+}