9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit 5b0c7727acfbfbabaa92c98e9fcbfd05bdb53ab2
parent 2592dacde0f85370b96465084e873e3f2c3fdeda
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Mon, 31 May 2021 12:50:17 -0300

Updated guia03 exercises

Diffstat:
Mguia03/ex01.c | 2+-
Mguia03/ex02.c | 6++++--
Mguia03/ex04.c | 7+++----
Mguia03/ex05.c | 6++++--
Mguia03/ex06.c | 9+++------
Mguia03/ex07.c | 6+++---
Mguia03/ex08.c | 5+++--
Mguia03/ex08_modular.c | 13+++++++------
Mguia03/ex09_modular.c | 48++++++++++++++++++------------------------------
9 files changed, 46 insertions(+), 56 deletions(-)
diff --git a/guia03/ex01.c b/guia03/ex01.c
@@ -12,7 +12,7 @@ int main(void) {
     puts(s);
 
     printf("> ");
-    // gets(s); removed from std C11 because it's very unsecure
+    /* gets(s); removed from std C11 because it's very unsecure */
     fgets(s, MAX_LEN, stdin);
     puts(s);
 
diff --git a/guia03/ex02.c b/guia03/ex02.c
@@ -7,13 +7,15 @@
 int main(void) {
     
     char s[MAX_LEN];
-    if(fgets(s, MAX_LEN, stdin) == NULL) {
+
+    if(!fgets(s, MAX_LEN, stdin)) {
         fprintf(stderr, ERR_EMPTY_MSG"\n");
         return 1;
-    } else if(strcmp(s, "") == 0) {
+    } else if(!strcmp(s, "")) {
         fprintf(stderr, ERR_EMPTY_MSG"\n");
         return 1;
     }
+
     printf("%s", s);
     return 0;
 }
diff --git a/guia03/ex04.c b/guia03/ex04.c
@@ -4,11 +4,10 @@
 int main(void) {
 
     char s[] = "Hello world!\n";
-    char aux;
-    int n;
+    int i, aux;
 
-    for(n = strlen(s); n >= 0; n--) {
-        aux = s[n];
+    for(i = strlen(s); i >= 0; i--) {
+        aux = s[i];
         if(aux != '\n')
             putchar(aux);
     }
diff --git a/guia03/ex05.c b/guia03/ex05.c
@@ -6,8 +6,10 @@ int main(void) {
 
     char s[] = "Hello world!\n";
     int i;    
-    for(i = 0; s[i] != '\0'; i++) {
-    }
+
+    for(i = 0; s[i]; i++)
+        ;
+
     printf("%d\n", i);
     
     return 0;
diff --git a/guia03/ex06.c b/guia03/ex06.c
@@ -7,18 +7,15 @@ int main(void) {
 
     char origen[] = "Hello world!\n";
     size_t i, len;
+
     len = strlen(origen);    
-    char destino[len];
+    char destino[len]; /*    +1 for the '\0'    */
 
-    for(i = 0; i <= len; i++) {
+    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/ex07.c b/guia03/ex07.c
@@ -2,13 +2,13 @@
 #include <string.h>
 
 
-int main(void) {
+int main(void)
+{
+    int i, s1_len, s2_len, cat_len;
 
     char s1[] = "Hello world!";
     char s2[] = "Hola mundo!";
 
-
-    int i, s1_len, s2_len, cat_len;
     s1_len = strlen(s1);
     s2_len = strlen(s2);
 
diff --git a/guia03/ex08.c b/guia03/ex08.c
@@ -5,8 +5,8 @@ size_t str_len(const char *str) {
     if(str == NULL)
         return 1;
 
-    size_t i;
-    for(i = 0; str[i] != '\0'; i++)
+    size_t i = 0;
+    while(str[i++])
         ; 
 
     return i;
@@ -33,6 +33,7 @@ int main(void) {
                 cmp += (s1[i] - s2[i]);
             }
     }    
+
     printf("%d\n", cmp);
     return 0;
 }
diff --git a/guia03/ex08_modular.c b/guia03/ex08_modular.c
@@ -9,8 +9,9 @@ size_t str_len(const char *str)
 {
     if(str == NULL)
         return 1;
+
     size_t i;
-    for(i = 0; str[i] != '\0'; i++) 
+    while(str[i++])
         ;
 
     return i;
@@ -20,7 +21,7 @@ size_t str_len(const char *str)
     <string.h> library                    */
 int str_cmp(const char *str1, const char *str2) 
 { 
-//    In case one of the given strings is NULL returns 1; 
+    /* In case one of the given strings is NULL returns 1; */ 
     if(str1 == NULL || str2 == NULL)
         return 1;
 
@@ -30,17 +31,17 @@ int str_cmp(const char *str1, const char *str2)
     str1_len = str_len(str1);
     str2_len = str_len(str2);
 
-//    Assigns the length of the longer string to j;
+    /* 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
+    /* 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
+        /* 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) {
diff --git a/guia03/ex09_modular.c b/guia03/ex09_modular.c
@@ -3,58 +3,46 @@
 /*    similar to strncmp() function from <string.h>            */
 
 #include <stdio.h>
+#include <string.h>
 
 /*    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;
-
     size_t i;
-    for(i = 0; str[i] != '\0'; i++) 
-        ;
+
+    if(!str) return 1;
+
+    for(i = 0; str[i]; i++);
 
     return i;
 }
 
 /*  This function is equivalent to strncmp() function from   
     <string.h> library                                      */
-int str_cmp(const char *str1, const char *str2, size_t n)
+int str_cmp(const char *sa, const char *sb, size_t n)
 { 
-    if(str1 == NULL || str2 == NULL)
-        return 1;
+    /*    Since the return value is an integer this function won't check for errors    */
+    int cmp;
+    size_t i;
+
+    for(i = cmp = 0; (i < n) && sa[i] && sb[i]; i++)
+        cmp += sa[i] - sb[i];
 
-    char aux1, aux2;
-    int i, j, cmp, str1_len, str2_len;
-    str1_len = str_len(str1);
-    str2_len = str_len(str2);
-    
-    if(n > str1_len || n > str2_len)
-        return 1;
-
-    for(i = 0, cmp = 0; i <= n; i++) {
-        aux1 = str1[i];
-        aux2 = str2[i];
-        if(aux1 < aux2) {
-            cmp -= (aux2 - aux1);
-        } else if (aux1 > aux2) {
-                cmp += (aux1 - aux2);
-            }
-    }
     return cmp;
 }
 
     
 int main(void) {
 
-    char s1[] = "AB ";
-    char s2[] = "ABC";
+    char s1[] = "HELLO";
+    char s2[] = "hello";
 
-    int i;
-    i = str_cmp(s1, s2, 4);
+    int i, j;
+    i = str_cmp(s1, s2, 109);
+    j = strncmp(s1, s2, 8);
 
-    printf("%d\n", i);
+    printf("%d\n%d\n", i, j);
     return 0;
 }