Advertisement
GastonFontenla

Untitled

May 31st, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "myString.h"
  5.  
  6. #define ES_LETRA(x) (('a' <= (x) && (x) <= 'z') || ('A' <= (x) && (x) <= 'Z'))
  7. #define MAYUSCULA(x) (('a' <= (x) && (x) <= 'z') ? ((x)-'a'+'A') : (x))
  8. #define MINUSCULA(x) (('A' <= (x) && (x) <= 'Z') ? ((x)-'A'+'a') : (x))
  9. #define ES_FINAL(x) ((x) == '\0')
  10.  
  11. ///Devuelve primera aparición de ch
  12. char *str_chr(const char *cadena, int ch)
  13. {
  14.     char *str = (char*)cadena;
  15.  
  16.     while(*str != '\0')
  17.     {
  18.         if(*str == ch)
  19.         {
  20.             return str;
  21.         }
  22.         str++;
  23.     }
  24.  
  25.     return NULL;
  26. }
  27.  
  28. char *normalizar(const char *cad)
  29. {
  30.     char *str = malloc(sizeof(char)*strlen(cad));
  31.     char *inicio = str;
  32.  
  33.     ///Saltar todos los carácteres inválidos al inicio
  34.     while(*cad && *cad == ' ')
  35.         cad++;
  36.  
  37.     int primerPalabra = 1;
  38.     int comaEncontrada = 0;
  39.  
  40.     while(*cad)
  41.     {
  42.         int primerLetra = 1;
  43.         while(*cad && ES_LETRA(*cad))
  44.         {
  45.             if(!primerPalabra && primerLetra == 1)
  46.             {
  47.                 *str = ' ';
  48.                 str++;
  49.             }
  50.  
  51.             if(primerLetra)
  52.                 *str = MAYUSCULA(*cad);
  53.             else
  54.                 *str = MINUSCULA(*cad);
  55.  
  56.             str++;
  57.             cad++;
  58.  
  59.             primerLetra = 0;
  60.         }
  61.         primerPalabra = 0;
  62.  
  63.         while(*cad && (*cad == ' ' || *cad == ','))
  64.         {
  65.             if(*cad == ',' && !comaEncontrada)
  66.             {
  67.                 comaEncontrada = 1;
  68.                 *str = ',';
  69.                 str++;
  70.             }
  71.             cad++;
  72.         }
  73.     }
  74.  
  75.     *str = '\0';
  76.  
  77.     return inicio;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement