Advertisement
Guest User

paste

a guest
Oct 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #define MAS +
  5.  
  6. char* substr(const char* str, size_t n){
  7.     char* nuevo_string = malloc(sizeof(char) * n+1);
  8.     if (nuevo_string == NULL){
  9.         return NULL;
  10.     }
  11.     strncpy(nuevo_string,str,n);
  12.     nuevo_string[n] = '\0';
  13.     return nuevo_string;
  14. }
  15.  
  16.  
  17. char* asignar_str(const char* str, int inicio, int final){
  18.     char* temporal = substr(str+inicio,final-inicio);
  19.     if (temporal == NULL){
  20.         return NULL;
  21.     }
  22.     return temporal;
  23. }
  24.  
  25.  
  26. char **split(const char *str, char sep){
  27.     int count = 0;
  28.     for(int i = 0; str[i] != '\0'; i++){
  29.         if (str[i] == sep){
  30.             count += 1;
  31.         }
  32.     }
  33.  
  34.     char **strv = malloc(sizeof(char*) * (count+2));
  35.     if (strv == NULL){
  36.         return NULL;
  37.     }
  38.  
  39.     int inicio = 0;
  40.     int fin = 0;
  41.     int posicion = 0;
  42.     for(int j = 0; j < strlen(str);j++){
  43.         if (str[j] != sep){
  44.             fin++;
  45.         }else{
  46.             strv[posicion] = asignar_str(str,inicio,fin);
  47.             posicion++;
  48.             fin++;
  49.             inicio = fin;
  50.         }
  51.     }
  52.     strv[posicion] = asignar_str(str,inicio,fin);
  53.     strv[posicion+1] = NULL;
  54.     return strv;
  55. }
  56.  
  57.  
  58. char *join(char **strv, char sep){
  59.     if(strv[0] == NULL){
  60.         char* cadena_vacia = calloc(1,sizeof(char));
  61.         if(!cadena_vacia){
  62.             return NULL;
  63.         }
  64.         strcpy(cadena_vacia,"");
  65.         return cadena_vacia;
  66.     }
  67.  
  68.     char chequeo_separador[1];
  69.     *chequeo_separador = sep;
  70.     int separador_nulo = strcmp(chequeo_separador,"\0");
  71.     int count = 0;
  72.     size_t letras = 0;
  73.  
  74.     while (strv[count] != NULL){
  75.         letras += strlen(strv[count]);
  76.         count++;
  77.     }
  78.  
  79.     if (separador_nulo == 0) letras -= (count-1);
  80.  
  81.     char* cadena_unida = calloc((letras+count),sizeof(char));
  82.     if (cadena_unida == NULL){
  83.         return NULL;
  84.     }
  85.     char* cadena_devolver = &cadena_unida[0];
  86.  
  87.     for(int i = 0; i<count;i++){
  88.         letras = 0;
  89.         size_t cantidad = strlen(strv[i]);
  90.         strcpy(cadena_unida,strv[i]);
  91.         if(separador_nulo != 0 && i<count-1){
  92.             *(cadena_unida+cantidad) = sep;
  93.             letras += 1;
  94.         }
  95.         letras += cantidad;
  96.         cadena_unida+=letras;
  97.     }
  98.     return cadena_devolver;
  99. }
  100.  
  101.  
  102. int main() {
  103.   char** operators = split("+ - * sqrt ^ ? log",' ');
  104.   char** cont = split("5 10 +", ' ');
  105.   for(int i = 0;i<7;i++){
  106.     if(strstr(operators[i],cont[2]) == 0){
  107.     printf("operador\n");
  108.     }
  109.   }
  110.   return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement