4

I want to create random data for testing. I want to fill an array with 100 strings of random length with the letter 'A'.

example:

array[0] = "AAAAA"
array[1] = "AAAAAAAA"
array[2] = "A"
...

char **create_string()
{
    char **array = malloc(sizeof(**array)); 

    srand((unsigned int)time(NULL));
    int random = 0;

    int i, j;
    for(int i=0; i<100; i++)
    {
        random = rand() % 100;
        for(j=0; j < random; j++)
        {
           array[i] = // some sort of string append that would be cheap.
        }
    }
}

I was looking at this C string append and they use strcat. Is there a better way to solve my problem? Since I will be running in a loop to create those random size strings.

Community
  • 1
  • 1
Mike John
  • 818
  • 4
  • 11
  • 29

6 Answers6

3
#include  <stdio.h>
#include <stdlib.h>
#include <time.h>
char **create_string(size_t n) {
    char **array = malloc(sizeof(char*) * n); 
    int i, j;
    for(i=0; i<100; i++)
    {   
        size_t sz = rand() % 100;
        array[i] = malloc(sz + 1); 
        for(j=0; j < sz; j++) {
            array[i][j] = 'A';
        }
        array[i][sz] = 0;
    }   
    return array;
}

int  main() {
    char **array;
    size_t i;
    srand((unsigned int)time(NULL));
    array = create_string(100);
    for (i = 0; i < 100; i++)
        printf("%s\n", array[i]);
    return 0;    

} 

Alternatively, you can create a template string and copy required number of characters into each random string:

char **create_string(size_t n) {
    char template[101];
    char **array = malloc(sizeof(char*) * n); 
    int i;

    for (i = 0; i < 100; i++)
        template[i] = 'A';
    template[100] = 0;
    for(i = 0; i < n; i++) {
        size_t sz = rand() % 100;
        array[i] = malloc(sz + 1); 
        strncpy(array[i], template, sz);
        array[i][sz] = 0;
    }   
    return array;
}
noufal
  • 940
  • 3
  • 15
  • 32
perreal
  • 94,503
  • 21
  • 155
  • 181
0

This will depend on the distribution of string lengths that you want. This is a uniform distribution of string lengths, from 0 to 200.

int n = rand() % 200 * sizeof(*array);
array[i] = malloc(n + 1);
memset(array[i], 'A', n);
array[i][n] = '\0';

But you could have a Gaussian distribution, a Poisson distribution, etc.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
0
 char **create_string()
  {
  char **array = malloc(sizeof(char *) * 100);

             srand((unisgned int)time(NULL));

        int i;
        for (i = 0; i <100;i++)
        {
            int random = rand() % 100;
            array[i] = malloc(random);
            memset(array[i],'A',random-1);
            array[random-1] = '\0';
        }
        return array;
    }

Problem for you to fix: what happens if random is 0? Also the random numbers will not be equaly distributed. Only modulo by a power of 2 will achieve that.

Myforwik
  • 3,438
  • 5
  • 35
  • 42
0

Here's an approach that doesn't put an upper bound on any individual string, but does put an exact bound on the total length of all strings. It also only calls malloc twice.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TOT_SIZE (5000) /* adjust to taste */
#define TOT_STRS (100)

char **create_strings()
{
    char **array = (char**) malloc(TOT_STRS * sizeof(char *));
    char *str    = (char*)  malloc(TOT_SIZE);
    int  zeros   = 1;
    int  i;

    memset(str, 'A', TOT_SIZE - 1);

    str[TOT_SIZE - 1] = 0;

    while (zeros < TOT_STRS)
    {
        int pos = rand() % TOT_SIZE;

        if (str[pos] != 0)
        {
            str[pos] = 0;
            zeros++;
        }
    }

    array[0] = str;

    for (i = 1; i < TOT_STRS; i++)
    {
        array[i] = array[i - 1] + strlen(array[i - 1]) + 1;
    }

    return array;
}

And a short test program:

int main()
{
    char **a = create_strings();
    int i;

    for (i = 0; i < TOT_STRS; i++)
    {
        printf("%3d: %s\n", i, a[i]);
    }

    return 0;
}

This code assumes that all the random strings need to be non-overlapping. If they can overlap in memory, you only need one string, and an array of pointer to different starting points in that one string.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
0

You do not have to allocate the real 100 strings. Firstly you just declare a long enough array char long_array[100]. and then you use random = rand() % 100; get the random. Secondly you just pass the long_array and the random to your function. Then your problem is solved.

yanchong
  • 276
  • 1
  • 9
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

char **create_string(const size_t array_size,
               const size_t string_size,
               const unsigned char chr)
{

    srand((unsigned)time(NULL));
    char ** array = malloc(array_size * sizeof (char *));

    size_t t;

    for (t = 0; t < array_size; ++t) {
        array[t] = malloc(string_size * sizeof(char));
        array[t][string_size] = '\0';
        memset(array[t], chr, (rand() % string_size) + 1);
    }

    return array;   
}

main() {

    char ** randstring = create_string(10, 7, 'A');
    int t = 0;
    for (; t < 10; ++t)
        printf("%s\n", randstring[t]);
    return 0;
}

Possible output

AAAAAA
AAAAA
AAAAAA
AAA
AAAAA
AAAAAA
AAAAAA
AAAAAAA
AAAA
AAAA
smac89
  • 39,374
  • 15
  • 132
  • 179