9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia08/ex01.c (1243B)
   1 /* Clones a given string entered as arguments of function main; */
   2 #include <stdio.h>
   3 #include <stdlib.h>
   4 #include <string.h>
   5 
   6 #define NO_ARGUMENT          1
   7 #define INIT_SIZE        100
   8 
   9 typedef enum {
  10     OK,
  11     ERROR_PROGRAM_INVOCATION,
  12     ERROR_NULL_POINTER,
  13     ERROR_ALLOC_MEMORY
  14 } status_t;
  15 
  16 status_t validar_argumentos(int argc, char **argv);
  17 
  18 int main (int argc, char * argv[]) 
  19 {
  20     char *dest;
  21     size_t i;
  22     status_t st;
  23 
  24     /* Verify if arguments are right; */
  25     if((st = validar_argumentos(argc, argv))) return st;
  26 
  27     /* Allocates memmory in heap of size INIT_SIZE; */
  28     if(!(dest = (char *)malloc(INIT_SIZE * sizeof(char))))
  29         return ERROR_ALLOC_MEMORY;
  30 
  31     /* Assigns 1 to i to avoid the first element of argv, which is the program name. Then copies every string of argv into dest and prints it on stdout; */
  32     for(i = 1; (int)i < argc; i++) {
  33         strcpy(dest, argv[i]);
  34         /* Puts a space in between strings, avoiding a blank space after first string is printed; */
  35         if(i != 1) putchar(' ');
  36         printf("%s", dest);
  37     }
  38 
  39     /* Adds the new line character; */
  40     putchar('\n');
  41     free(dest);
  42     return OK;
  43 }
  44 
  45 status_t validar_argumentos(int argc, char **argv) 
  46 {
  47     if(argc == NO_ARGUMENT) return ERROR_PROGRAM_INVOCATION;
  48     if(!argv) return ERROR_NULL_POINTER;
  49 
  50     return OK;
  51 }