Add a character to the list n times

I want to create random data for testing. I want to fill an array with 100 lines of random length with a 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 looked at this line of C append and they use strcat. Is there a better way to solve my problem? Since I will work in a loop to create these strings of random size.

+4
source share
6 answers
#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 line and copy the required number of characters into each arbitrary line:

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;
}
+3
source

This will depend on the string length distribution 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';

, ..

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;
    }

: , 0? . 2 .

0

, , . malloc .

#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;
}

:

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

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

    return 0;
}

, . , .

0

You do not need to highlight the real 100 lines. First, you simply declare a fairly long array char long_array[100]. and then you use random = rand() % 100;get random. Secondly, you just pass long_arrayand randomin its function. Then your problem is resolved.

0
source
#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 way out

AAAAAA
AAAAA
AAAAAA
AAA
AAAAA
AAAAAA
AAAAAA
AAAAAAA
AAAA
AAAA
0
source

All Articles