Hello -
I've tokenized a string of characters, but now I am trying to figure out how I can display the tokens randomly on the screen. Thanks for your help!
Hello -
I've tokenized a string of characters, but now I am trying to figure out how I can display the tokens randomly on the screen. Thanks for your help!
How randomly do you need them? If it's only a matter of printing any string to the screen without worry about the same one being printed more than once then this would work fine:
-PreludeCode:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static void printRandom ( const char *c[], int size ) { int i; for ( i = 0; i < size; i++ ) printf ( "%s\n", c[( rand() * size ) / RAND_MAX] ); } int main ( void ) { int i; char a[] = "Everybody loves working with C-Strings, right?", copy[BUFSIZ], *b[BUFSIZ] = {0}, *tok; srand ( (unsigned)time ( NULL ) ); strcpy ( copy, a ); tok = strtok ( copy, " " ); for ( i = 0; tok != NULL; i++ ) { b[i] = tok; tok = strtok ( NULL, " " ); } printRandom ( b, i ); return EXIT_SUCCESS; }
My best code is written with the delete key.
I don't like to use strtok because it requires a lot more care to keep error free. So here is my alternative:
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> //i hate strtok.... char **makemultistr(char *str, int *size) { int i, n; char *ptr1, *ptr2, **rtrn_str; for(ptr1 = strstr(str, " "), i = 0; ptr1 != NULL; i++, ptr1 = strstr(ptr1, " ")) { if(ptr1 != NULL) ptr1++; //i will hold the number of tokens after the for loop is done } *size = i; rtrn_str = (char **)malloc(i*sizeof(char *)); if(rtrn_str != NULL) { for(ptr1 = strstr(str, " "), i = 0; ptr1 != NULL; i++, ptr1 = strstr(ptr1, " ")) { if(ptr1 != NULL) ptr1++; ptr2 = strstr(ptr1, " "); n = (ptr2 != NULL)?ptr2-ptr1:strlen(ptr1); rtrn_str[i] = (char *)malloc(n); if(rtrn_str[i] != NULL) strncpy(rtrn_str[i], ptr1, n); else fprintf(stderr, "malloc() error.\r\n"); } } else fprintf(stderr, "malloc() error.\r\n"); return rtrn_str; } void freemultistr(char **str, int size) { int i; for(i = 0; i < size; i++) free(str[i]; free(str); } int main(void) { char str[] = "This is much easier to do now...", **str2d; int size, i; str2d = makemultistr(str, &size); srand(time(NULL)); //set random seed if(str2d == NULL) return 1; while(1) //infinite loop printf("%s\r\n", str2d[(long)(rand()%time(NULL))%size]); //if this loop weren't infinite it would be a good idea to do this freemultistr(str2d); return 0; }
>I don't like to use strtok because it requires a lot more care to keep error free.
To each their own, though you'll rarely have problems with strtok if you take the proper care in using it.
-Prelude
My best code is written with the delete key.
>>To each their own, though you'll rarely have problems with strtok if you take the proper care in using it.
The other reason I don't like using it is because you have two options with strtok. Make a copy of the string that will be tokenized or allow the input string to be altered. The fact that the string is altered is the main reason that I don't like strtok. For most applications copying a string is no big deal (my function pretty much did that) but there are times when you will be dealing with a large string and copying it isn't really an option.
I guess you are right: "To each their own."