c-first-steps

a C playground
Index Commits Files Refs README
commit 313069e1792b40fe04c3c12fed44eb46fd0f84ba
parent 0ef0e82f58b61734d34d02c9f9197e34077a1d86
Author: Martin J. Klockner <martin.cachari@gmail.com>
Date:   Wed, 18 Nov 2020 12:52:12 -0300

Added ex10.c,ex11.c,ex12.c & Updated ej8_modular.c & ej9_modular source
code, to improve readability

Diffstat:
M95.11/guia03/ej8_modular.c | 35++++++++++++++++++++++-------------
M95.11/guia03/ej9_modular.c | 19++++++++++++++-----
A95.11/guia03/ex10.c | 23+++++++++++++++++++++++
A95.11/guia03/ex11.c | 19+++++++++++++++++++
A95.11/guia03/ex12.c | 19+++++++++++++++++++
5 files changed, 97 insertions(+), 18 deletions(-)
diff --git a/95.11/guia03/ej8_modular.c b/95.11/guia03/ej8_modular.c
@@ -1,12 +1,12 @@
 /*    Makes a lexicographical comparison between two strings    */
-/*    similar to strlen() function from <string.h>            */
+/*    similar to strcmp() function from <string.h>            */
 
 #include <stdio.h>
 
-
 /*    This function calculates the length of given string
-    and returns it by its name                                */
-size_t str_len(const char *str) {
+    and returns the lenghth by its name as a size_t            */
+size_t str_len(const char *str) 
+{
     if(str == NULL)
         return 1;
     size_t i;
@@ -16,42 +16,51 @@ size_t str_len(const char *str) {
     return i;
 }
 
-/*    This function is equivalent to strlen() function from    
+/*    This function is equivalent to strcmp() function from    
     <string.h> library                                        */
-int str_cmp(const char *str1, const char *str2) { 
+int str_cmp(const char *str1, const char *str2) 
+{ 
+    //    In case one of the given strings is NULL returns 1; 
     if(str1 == NULL || str2 == NULL)
         return 1;
 
+
     char aux1, aux2;
     int i, j, cmp, str1_len, str2_len;
     str1_len = str_len(str1);
     str2_len = str_len(str2);
-    
-    j = (str1_len > str2_len) ? str1_len : str2_len;
 
+    //    Assigns the length of the longer string to j;
+    j = (str1_len > str2_len) ? str1_len : str2_len; 
+
+    //    The for loop itinirate until the longest str ENDs
     for(i = 0, cmp = 0; i < j; i++) {
+
         aux1 = str1[i];
         aux2 = str2[i];
+
+        //    This statement assigns to cmp the difference between
+        //    the two coresponding chars of str1 and str2
         if(aux1 < aux2) {
             cmp -= (aux2 - aux1);
-        } else 
-            if (aux1 > aux2) {
+        } else if (aux1 > aux2) {
                 cmp += (aux1 - aux2);
             }
-    }
 
+    }
     return cmp;
 }
 
     
 int main(void) {
 
-    char s1[] = "AB ";
+    char s1[] = "AB";
     char s2[] = "ABC";
 
-    int i;
+    int i, j;
     i = str_cmp(s1, s2);
 
     printf("%d\n", i);
+    
     return 0;
 }
diff --git a/95.11/guia03/ej9_modular.c b/95.11/guia03/ej9_modular.c
@@ -1,6 +1,14 @@
+/*  Makes a lexicographical comparison between the first n    */
+/*    numbers of two given strings                            */
+/*    similar to strncmp() function from <string.h>            */
+
 #include <stdio.h>
 
-size_t str_len(const char *str) {
+/*    This function calculates the length of given string
+    and returns it by its name, equivalent to strlen()        
+    function from <string.h> library                        */
+size_t str_len(const char *str)
+{
     if(str == NULL)
         return 1;
 
@@ -11,7 +19,10 @@ size_t str_len(const char *str) {
     return i;
 }
 
-int str_cmp(const char *str1, const char *str2, size_t n) { 
+/*  This function is equivalent to strncmp() function from   
+    <string.h> library                                      */
+int str_cmp(const char *str1, const char *str2, size_t n)
+{ 
     if(str1 == NULL || str2 == NULL)
         return 1;
 
@@ -28,12 +39,10 @@ int str_cmp(const char *str1, const char *str2, size_t n) {
         aux2 = str2[i];
         if(aux1 < aux2) {
             cmp -= (aux2 - aux1);
-        } else 
-            if (aux1 > aux2) {
+        } else if (aux1 > aux2) {
                 cmp += (aux1 - aux2);
             }
     }
-
     return cmp;
 }
 
diff --git a/95.11/guia03/ex10.c b/95.11/guia03/ex10.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <string.h>
+
+
+
+int main(void) {
+
+    char src[] = "Hello world!\n";
+    size_t i, n;
+
+    n = 12;
+    char cpy[n];
+
+    for(i = 0; i <= n; i++) {
+        cpy[i] = src[i];
+    }
+
+    printf("%s", src);
+    printf("%s", cpy);
+
+
+    return 0;
+}
diff --git a/95.11/guia03/ex11.c b/95.11/guia03/ex11.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <ctype.h>
+
+int main(void) 
+{
+    char str[] = "hello world";
+    int i;
+    char aux;
+
+    for(i = 0; (aux = str[i]) != '\0'; i++)
+        if(islower(aux))
+            str[i] = toupper(aux);
+
+    printf("%s\n", str);
+    return 0;
+}
+
+
+
diff --git a/95.11/guia03/ex12.c b/95.11/guia03/ex12.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <ctype.h>
+
+int main(void) 
+{
+    char str[] = "HELLO WORLD";
+    int i;
+    char aux;
+
+    for(i = 0; (aux = str[i]) != '\0'; i++)
+        if(isupper(aux))
+            str[i] = tolower(aux);
+
+    printf("%s\n", str);
+    return 0;
+}
+
+
+