9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia08/ex15.c (457B)
   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <string.h>
   4 
   5 char *strclone(char *);
   6 
   7 int main (void) {
   8     char str[] = "Hello, world!";
   9     char *cpy;
  10 
  11     if(!(cpy = strclone(str))) return 1;
  12 
  13     printf("%s\n", cpy);
  14 
  15     free(cpy);
  16     return 0;
  17 }
  18 
  19 char *strclone(char *s)
  20 {
  21     if(!s) return NULL;
  22 
  23     char *c;
  24 
  25     /* +1 for the null character */
  26     if(!(c = (char *)calloc(sizeof(char), (strlen(s) + 1)))) return NULL;
  27 
  28     for(size_t i = 0; *s; s++, i++) c[i] = *s;
  29 
  30     return c;
  31 }