String is not being filled with random chars (C)? -


i'm trying fill string characters string 'reset\0' randomized. reason gives me 1 character back:

#define str_len 6 char *inputstring() {     char *string[str_len + 1] = {0};     const char *digits = "reset\0";     int i;      (i = 0; < str_len; i++)      {         string[i] = digits[ rand() % 5 + 0 ];     }      printf("string: %s\n", string); } 

prints 1 character 't' or 'e' console. doing wrong?

if mean making random permutation of characters, not using source string alphabet, consider fisher–yates shuffle. implementation this:

char s[] = "reset"; (size_t = strlen(s) - 1; > 0; i--) {     size_t j = rand() % (i + 1);     char t = s[i];     s[i] = s[j];     s[j] = t; } printf("%s\n", s); 

the idea go right left, , on every step swap element @ index i element @ random index j between 0 , i, inclusive.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -