2013-11-01 7 views
8

mia stringa è "A, B, C, D, E"
e il separatore è ""
Come posso ottenere la stringa restante dopo aver fatto strtok() una volta, cioè "B, C, D, E"C: Come ottenere la stringa rimanente dopo l'utilizzo di strtok() una volta

char a[] = "A,B,C,D,E"; 
char * separator = ","; 
char * b = strtok(a,separator); 
printf("a: %s\n", a); 
printf("b: %s\n", b); 

l'output è:
un: un
B: un

Ma come ottenere il risultato
a: B, C, D, E
b: A

Grazie.

+0

Non pensare 'strtok()' fa. – Rohan

risposta

7

Non utilizzare strtok() per questo, poiché non è quello che serve.

Usa strchr() per trovare il primo separatore, e passare da lì:

char a[] = "A,B,C,D,E"; 
const char separator = ','; 
char * const sep_at = strchr(a, separator); 
if(sep_at != NULL) 
{ 
    *sep_at = '\0'; /* overwrite first separator, creating two strings. */ 
    printf("first part: '%s'\nsecond part: '%s'\n", a, sep_at + 1); 
} 
+0

Soluzione molto cleaver! –

1

Prova questo:

char a[] = "A,B,C,D,E"; 
char * end_of_a = a + strlen(a); /* Memorise the end of s. */ 
char * separator = ","; 
char * b = strtok(a, separator); 
printf("a: %s\n", a); 
printf("b: %s\n", b); 

/* There might be some more tokenising here, assigning its result to b. */ 

if (NULL != b) 
{ 
    b = strtok(NULL, separator); 
} 

if (NULL != b) 
{ /* Get reference to and print remainder: */ 
    char * end_of_b = b + strlen(b); 

    if (end_of_b != end_of_a) /* Test whether there really needs to be something, 
         will say tokenising did not already reached the end of a, 
         which however is not the case for this example. */ 
    { 
    char * remainder = end_of_b + 1; 
    printf("remainder: `%s`\n", remainder); 
    } 
} 
0

Se si utilizza strtok non è un requisito, è possibile utilizzare strchr invece dal separatore è un singolo carattere:

char a[] = "A,B,C,D,E"; 
char *sep = strchr(a, ','); 
*sep = '\0'; 
puts(a); 
puts(sep + 1); 
0

printf ("a:% s \ n", a + 1 + strlen (b));

Prova questa

+0

Vedere * larsmans * commenti alla risposta * dasblinkenlight * su questo approccio. Vale anche per la tua risposta. – alk

0

strtok ricorda l'ultima stringa ha funzionato con e dove è finito. Per ottenere la stringa successiva, chiamala di nuovo con NULL come primo argomento.

char a[] = "A,B,C,D,E"; 
const char *separator = ","; 
char *b = strtok(a, separator); 
while (b) { 
    printf("element: %s\n", b); 
    b = strtok(NULL, separator); 
} 

Nota: questo non è sicuro per thread.

15

è possibile variare il set di delimitatori, così semplicemente passare una stringa vuota:

char a[] = "A,B,C,D,E"; 
char * separator = ","; 
char * b = strtok(a, separator); 
printf("b: %s\n", b); 
char * c = strtok(NULL, ""); 
printf("c: %s\n", c); 
+1

** Il passaggio da nessun delimitatore è la strada da percorrere! ** Nel mio caso, ho usato 'strtok' più di una volta e ad un certo punto avevo bisogno del resto. Quindi ** non ** usare 'strtok' non era una soluzione. Né sommare i caratteri perché stavo "giocando" nello spazio, e potrebbe contenere un numero variabile di spazi consecutivi, ecc. – Paschalis

Problemi correlati