Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "strutil.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- char** array_redimensionar(char** array,size_t* capacidad){
- char** array_aux = realloc(array,sizeof(char*) * (2 *(*capacidad)));
- *capacidad*=2;
- return array_aux;
- }
- char** split(const char* cadena, char sep){
- size_t capacidad = 10;
- size_t largo,i=0,condicion;
- char** array = malloc(sizeof(char*)*capacidad);
- char* sobrante;
- sobrante=strchr(cadena,sep);
- if(!sobrante)
- sobrante="";
- condicion = strlen(sobrante);
- while(condicion){
- if (i==capacidad-1)
- array=array_redimensionar(array,&capacidad);
- largo= strlen(cadena) - strlen(sobrante);
- array[i] = (char*)calloc(largo+1,sizeof(char));
- memcpy(array[i],cadena,largo);
- cadena=sobrante+1;
- sobrante=strchr(cadena,sep);
- if(!sobrante)
- sobrante="";
- condicion = strlen(sobrante);
- i++;
- }
- largo = strlen(cadena) + 1; // +1 por /0
- array[i] = malloc(largo * sizeof(char));
- memcpy(array[i],cadena,largo);
- array[i+1] = NULL;
- return array;
- }
- char* join(char** array,char sep){
- size_t letras=0;
- char** p=array;
- while(*p){
- letras=letras+strlen(*p)+1;
- p++;
- }
- letras++;
- int k=0;
- char* cadena = calloc(letras,sizeof(char));
- for(int i=0;array[i];i++){
- for(int j=0;array[i][j];j++){
- cadena[k] = array[i][j];
- k++;
- }
- if(array[i+1])
- cadena[k] = sep;
- k++;
- }
- return cadena;
- }
- void free_strv(char* array[]){
- for(int i = 0; array[i];i++)
- if(array[i])
- free(array[i]);
- free(array);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement