9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex08.c (533B)
   1 #include <stdio.h>
   2 #include <string.h>
   3 
   4 size_t str_len(const char *str) {
   5     if(str == NULL) return 1;
   6 
   7     size_t i = 0;
   8     while(str[i++]); 
   9 
  10     return i;
  11 }
  12 
  13 int main(void) {
  14 
  15     char s1[] = "ABC";
  16     char s2[] = "ABC";
  17 
  18     size_t s1_len, s2_len;
  19     int i, j, cmp;
  20 
  21     s1_len = str_len(s1);
  22     s2_len = str_len(s2);
  23     
  24     j = (s1_len > s2_len) ? s1_len : s2_len;
  25 
  26     for(i = 0, cmp = 0; i < j; i++) {
  27         if(s1[i] < s2[i]) {
  28             cmp -= (s2[i] - s1[i]);
  29         } else 
  30             if (s1[i] > s2[i]) {
  31                 cmp += (s1[i] - s2[i]);
  32             }
  33     }    
  34 
  35     printf("%d\n", cmp);
  36     return 0;
  37 }