9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex26.c (1210B)
   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 
   4 #define MAX_LEN 100
   5 #define ERR_MAX_STR_LEN "Err: the entered number is too big"
   6 
   7 
   8 int main ( void ) {
   9 
  10     char buffer[MAX_LEN], str1[MAX_LEN], str2[MAX_LEN];
  11     char dest[MAX_LEN];
  12     int i, j, num;
  13 
  14     if(fgets(str1, MAX_LEN, stdin) == NULL) {
  15         return 1;
  16     }
  17 
  18 //  This part counts the length of the first string
  19 //    and replaces the new line character for a blank space;
  20     for(i = 0; str1[i] != '\0'; i++)
  21         if(str1[i] == '\n')
  22             str1[i] = ' ';
  23             str1[i + 1] = '\0';
  24 
  25     if(fgets(str2, MAX_LEN, stdin) == NULL) {
  26         return 1;
  27     }
  28 
  29     if(fgets(buffer, MAX_LEN, stdin) == NULL) {
  30        return 1;
  31     }
  32 
  33     num = atoi(buffer);
  34 
  35 //  This part checks if the input number is bigger than
  36 //  the length of the first string;
  37     if(num > i) {
  38         fprintf(stderr, ERR_MAX_STR_LEN"\n");
  39         return 1;
  40     }
  41 
  42 
  43 //    Removes the new line chracter from string two;
  44     for(i = 0; str2[i] != '\0'; i++)
  45         if(str2[i] == '\n')
  46             str2[i] = '\0';
  47 
  48     for(i = 0, j = 0; str2[j] != '\0'; i++) {
  49         if((i <= num) && (str1[i] != '\0')) {
  50             dest[i] = str1[i];
  51         } else { 
  52             dest[i] = str2[j];
  53             j++;
  54         }
  55     }
  56     printf("%s\n", dest);
  57     return 0;
  58 }