9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia08/ex03.c (614B)
   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <string.h>
   4 
   5 char *right_trim(const char *);
   6 
   7 int main (void)
   8 {
   9     char str[] = "Hola mundo     ";
  10     char *dst;
  11 
  12     printf("%s\n", str);
  13     printf("len %ld\n", strlen(str));
  14 
  15     dst = right_trim(str);
  16     printf("%s\n", dst);
  17     printf("len %ld\n", strlen(dst));
  18 
  19     free(dst);
  20     return 0;
  21 }
  22 
  23 char *right_trim(const char *src)
  24 {
  25     char *dst;
  26     size_t i;
  27 
  28     /* Counts blank spaces */
  29     for(i = strlen(src); src[i - 1] == ' '; i--);
  30 
  31     /* +1 for the null character */
  32     if(!(dst = (char *)malloc((i + 1) * sizeof(char))))
  33         return NULL;
  34 
  35     strncpy(dst, src, i);
  36     dst[i] = '\0';
  37     return dst;
  38 }