Enviado em 21/12/2005 - 17:31h
Fala ae pessoal eu to com uma dúvida aqui, alguêm conhece uma lib em C que faça um split estilo o do PHP ?
Enviado em 21/12/2005 - 17:31h
Enviado em 21/12/2005 - 18:16h
int strSplit(char *strTOsplit,char *strArr[], char *strSeparet,int nArr)
{
int i = 0;
char * pch;
pch = strtok (strTOsplit,strSeparet);
for(i = 0;i < nArr;i++)
{
//printf ("%s\n",pch);
strArr[i] = pch;
pch = strtok (NULL,strSeparet);
}
}
Enviado em 30/03/2008 - 21:34h
Enviado em 30/03/2008 - 22:26h
Enviado em 17/08/2009 - 11:42h
int ConvertStringHoraToSegundos(char* hora) {
char *vetor;
char *aux[]={"","",""};
char separador[] = {":"};
int total, hora_nsegundos, minuto_nsegundos, segundos;
system("cls");
printf("\nhora - %s\n",hora);
getchar();
strSplit(hora,aux,separador,3);
printf("\n\n\n vetor - %s \t aux - %s",vetor,aux);
getchar();
hora_nsegundos = atoi(vetor[0]);
minuto_nsegundos = atoi(vetor[1]);
segundos = atoi(vetor[2]);
total = (hora_nsegundos * 3600) + (minuto_nsegundos * 60) + segundos;
return total;
}
Enviado em 17/08/2009 - 12:09h
Enviado em 20/12/2011 - 12:31h
/* split.c
*
* Enzo Ferber
* dez 2011
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* poscpy()
*
* @t = target string (should be malloc(a + b + 1))
* @s = source string
* @a = startpoint in source string (must be > 0 )
* @b = endpoint in source string (must be < strlen(s))
*
* @function
* copys s[a] -> s[b] into t.
*
*/
void poscpy ( char *t, char *s, int a, int b )
{
while ( a < b ) *(t++) = *(s + (a++));
*t = 0x0;
}
char **split ( char *str, char n, int *length )
{
register int i, j, a;
/* control */
int len = strlen(str);
int elements = 0, elpos = 0;
char **array;
/* number of new itens in array */
for ( i = 0; i < len; i++ ) if ( str[i] == n ) elements++;
/* get the memory */
array = ( char ** ) calloc ( elements , sizeof(char *));
if ( !array )
{
printf ( "# Error in malloc(*).\n" );
return NULL;
}
/* the number of elements for the caller */
*length = elements;
/* lvl1
*
* @i = will be the start point for copy
*/
for ( i = 0; i < len; i++ )
/* lvl2
*
* @j = will be end point for copy
*/
for ( j = i; j <=len; j++ )
/* found splitChar or EoL */
if ( str[j] == n )
{
/*
* @i has start point
* @j has end point
*/
array[ elpos ] = ( char *) malloc ( (j - i + 1) * sizeof(char));
if ( !array[ elpos ] )
{
printf ( "# lvl2\n");
printf ( " # Error in malloc().\n" );
return NULL;
}
/* copy the string into the array */
poscpy ( array[ elpos ], str, i, j );
/* increment array position */
elpos++;
/* after the copy is done,
*
* @i must be equal to @j
*/
i = j;
/* end loop lvl2 */
break;
}
/* return array */
return array;
}
int main ( void )
{
int len, i;
char *str = "Enzo:de:Brito:Ferber:Viva:O:Linux:A:Maior:Comunidade:Linux:da:America:Latina!:";
char **elements = split(str, ':', &len);
// reference pointer
char **a = elements;
printf ( "# Elements: %d\n", len );
while ( *a ) printf (" # %s\n", *(a++));
return 0;
}
Enviado em 20/12/2011 - 12:39h
Enviado em 20/12/2011 - 12:57h
Enviado em 20/12/2011 - 14:06h
Enviado em 20/12/2011 - 14:08h
Enviado em 20/12/2011 - 14:25h
char **s = split("enzo:ferber:", ':', &len);
char **s = split("enzo:ferber", ':', &len );
Entre na sua conta para responder.