How to generate random string in C

5 Answers

0 votes
#include <stdio.h>     
#include <stdlib.h>
#include <time.h> 
  
static char *generate_random_string(char *str, size_t size);
  
int main()
{   
    char *s = NULL;
      
    srand((unsigned)time(NULL));
      
    s = generate_random_string(s, 10);
       
    if (s) {
        printf("%s\n", s);
        free(s);
    }
      
    return 0;
}
  
static char *generate_random_string(char *str, size_t size) {
    const char characters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
    int characters_count = sizeof characters;
      
    if (size) {
        str = malloc(size + 1);
        if (!str) {
            printf("malloc error\n");
            return 0;
        }
        
        for (size_t i = 0; i < size; i++) {
            str[i] = characters[rand() % characters_count];
        }
        
        str[size - 1] = '\0';
    }
     
    return str;
}
  
   
   
/*
run:
     
nEVJPpZ56F
  
*/

 

 



answered Apr 4, 2016 by avibootz
edited Feb 15, 2024 by avibootz
0 votes
#include <stdio.h>     
#include <stdlib.h>
#include <time.h> 
  
static char *generate_random_string(char *str, size_t size);
  
int main()
{   
    char *s = NULL;
      
    srand((unsigned)time(NULL));
      
    s = generate_random_string(s, 10);
       
    if (s) {
        printf("%s\n", s);
        free(s);
    }
      
    return 0;
}
  
static char *generate_random_string(char *str, size_t size) {
    const char characters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
    if (size == 0) return str;
      
    int characters_count = sizeof characters;
      
    str = (char*)malloc(size * sizeof(char));
    if (!str) {
        printf("malloc error\n");
        return 0;
    }
     
    char *p = str;
    while (size-- > 0) {
        *p++ = characters[rand() % characters_count];
    }
    *p = '\0';
      
    return str;
}
  
   
/*
run:
     
6wQ43wqgLR
  
*/

 



answered Apr 4, 2016 by avibootz
edited Feb 15, 2024 by avibootz
0 votes
#include <stdio.h>     
#include <stdlib.h>
#include <time.h> 
  
static char *generate_rand_string(char *str, size_t size);
  
#define SIZE 10
  
int main()
{   
    char *s = (char*)malloc(SIZE * sizeof(char));
      
    srand((unsigned)time(NULL));
       
    if (s) {
        s = generate_rand_string(s, SIZE);
        printf("%s\n", s);
        free(s);
    }
      
    return 0;
}
  
static char *generate_rand_string(char *str, size_t size) {
    const char characters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
    if (size == 0) return str;
      
    int characters_count = sizeof characters;
      
    char *p = str;
    while (size-- > 0)  {
        *p++ = characters[rand() % characters_count];
    }
    *p = '\0';
      
    return str;
}
  
   
/*
run:
     
76QHvj6Foe
  
*/

 

 



answered Apr 4, 2016 by avibootz
edited Feb 15, 2024 by avibootz
0 votes
#include <stdio.h>     
#include <stdlib.h>
#include <time.h> 

static char* generate_random_string(char* str, size_t size);

int main()
{
    char* s = NULL;

    srand((unsigned)time(NULL));

    s = generate_random_string(s, 10);

    if (s) {
        printf("%s\n", s);
        free(s);
    }

    return 0;
}

static char* generate_random_string(char* str, size_t size) {
    const char characters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*";

    if (size) {
        str = (char*)malloc(size * sizeof(char));
        if (!str) {
            printf("malloc error\n");
            return 0;
        }

        for (size_t i = 0; i < size; i++) {
            str[i] = characters[rand() % sizeof characters];
        }
        str[size - 1] = '\0';
    }

    return str;
}


/*
run:

Xb0nO5lK&@

*/

 



answered Oct 25, 2016 by avibootz
edited Feb 15, 2024 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  
void generate_random_string(char *str, size_t size) {
    const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    srand(time(0)); 
      
    for (int i = 0; i < size; i++) {
        str[i] = charset[rand() % (int) (sizeof charset - 1)];
    }
     
    str[size - 1] = '\0';
}
  
int main(void) {
    char s[15] = "";
      
    generate_random_string(s, 12);
  
    puts(s);
  
    return 0;
}
  
  
  
  
/*
run:
    
vaQFfatoQXl
    
*/

 



answered Feb 15, 2024 by avibootz
...