Originariamente inviata da
oregon
Scusa, ma hai detto di avere scritto il codice che tira fuori i token o no?
esatto, ho prima studiato la funzione dal libro, per capirne meglio l'utilizzo e poi ho seguito un esempio;
il programma gira infatti!
mi dice che la stringa 1 dopo che viene usata la funzione strtok diventa:
ciao
a
tutti
come
state?
proprio perchè viene suddivisa in token, stessa cosa per la stringa 2.
codice:
#include <stdio.h>
#include <string.h>
char string1[] = "ciao a tutti come state?";
char string2[] = "My test is here ";
printf("La stringa1 è: %s\n", string1);
printf("La stringa2 è: %s\n", string2);
printf("%s\n%s\n\n%s\n",
"The string to be tokenized is:", string1,
"The tokens are:");
char *tokenPtr1 = strtok(string1, " " );
while (tokenPtr1 != NULL)
{
printf("%s\n", tokenPtr1);
tokenPtr1 = strtok(NULL, " " );
}
printf("%s\n%s\n\n%s\n",
"\nThe string to be tokenized is:", string2,
"The tokens are:");
char *tokenPtr2 = strtok(string2, " " );
while (tokenPtr2 != NULL)
{
printf("%s\n",tokenPtr2);
tokenPtr2 = strtok(NULL, " " );
}
}
Io a questo punto mi domando se sia possibile confrontare il numero di elementi che vengono fuori dalle due stringhe dopo la funzione strtok...
Quale funzione potrei usare per far leggere al programma che la stringa1 ha "più parole" rispetto alla stringa 2?