9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex13.c (1129B)
   1 /*    Reads a string from stdin and converts it to uppercase or lowercase 
   2  *     depending on an option readed from stdin    */
   3 
   4 #include <stdio.h>
   5 #include <stdlib.h>
   6 #include <string.h>
   7 #include <ctype.h>
   8 
   9 #define MSG_STR_PROMPT    "Introduzca una cadena:"
  10 #define MSG_FMT_PROMPT    "Convertir a: (0 - MAYUSCULAS / 1 - MINUSCULAS)"
  11 #define MSG_FMT_ERR        "Err: Formato incorrecto"
  12 
  13 #define MAX_STR_LEN 100
  14 
  15 typedef enum {
  16     FMT_MAYUSCULAS, FMT_MINUSCULAS
  17 } format_t;
  18 
  19 
  20 int
  21 main (void)
  22 {
  23     char buffer[MAX_STR_LEN];
  24     char str[MAX_STR_LEN];
  25     format_t format;
  26 
  27     printf("%s%s", MSG_STR_PROMPT, "\n>> ");
  28     fgets(buffer, MAX_STR_LEN, stdin);
  29     strcpy(str, buffer);
  30 
  31     printf("%s%s", MSG_FMT_PROMPT, "\n>> ");
  32     format = (getchar () - '0');
  33     if((format < 0) || (format > 1)) {
  34         fprintf(stderr, "%s\n", MSG_FMT_ERR);
  35         return 1;
  36     }
  37 
  38     int i, aux;
  39     switch(format)
  40     {
  41         case FMT_MAYUSCULAS: 
  42             for(i = 0; (aux = str[i]) != '\n'; i++)
  43                 if(islower(aux))
  44                     str[i] = toupper(aux);
  45 
  46             break;
  47         case FMT_MINUSCULAS:
  48             for(i = 0; (aux = str[i]) != '\n'; i++)
  49                 if(isupper(aux))
  50                     str[i] = tolower(aux);
  51 
  52             break;
  53     }
  54 
  55     printf("%s", str);
  56     return 0;
  57 }