Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "myString.h"
- #define ES_LETRA(x) (('a' <= (x) && (x) <= 'z') || ('A' <= (x) && (x) <= 'Z'))
- #define MAYUSCULA(x) (('a' <= (x) && (x) <= 'z') ? ((x)-'a'+'A') : (x))
- #define MINUSCULA(x) (('A' <= (x) && (x) <= 'Z') ? ((x)-'A'+'a') : (x))
- #define ES_FINAL(x) ((x) == '\0')
- ///Devuelve primera aparición de ch
- char *str_chr(const char *cadena, int ch)
- {
- char *str = (char*)cadena;
- while(*str != '\0')
- {
- if(*str == ch)
- {
- return str;
- }
- str++;
- }
- return NULL;
- }
- char *normalizar(const char *cad)
- {
- char *str = malloc(sizeof(char)*strlen(cad));
- char *inicio = str;
- ///Saltar todos los carácteres inválidos al inicio
- while(*cad && *cad == ' ')
- cad++;
- int primerPalabra = 1;
- int comaEncontrada = 0;
- while(*cad)
- {
- int primerLetra = 1;
- while(*cad && ES_LETRA(*cad))
- {
- if(!primerPalabra && primerLetra == 1)
- {
- *str = ' ';
- str++;
- }
- if(primerLetra)
- *str = MAYUSCULA(*cad);
- else
- *str = MINUSCULA(*cad);
- str++;
- cad++;
- primerLetra = 0;
- }
- primerPalabra = 0;
- while(*cad && (*cad == ' ' || *cad == ','))
- {
- if(*cad == ',' && !comaEncontrada)
- {
- comaEncontrada = 1;
- *str = ',';
- str++;
- }
- cad++;
- }
- }
- *str = '\0';
- return inicio;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement