<分区>
我想生成一个仅由小写 ASCII 字符组成的随机字符串(意思是小写字母、数字和其他 ASCII 字符;只是没有大写字母)。
字符串的最大长度应为 587 个字符(包括空终止符)。
我该怎么做?
谢谢
<分区>
我想生成一个仅由小写 ASCII 字符组成的随机字符串(意思是小写字母、数字和其他 ASCII 字符;只是没有大写字母)。
字符串的最大长度应为 587 个字符(包括空终止符)。
我该怎么做?
谢谢
最佳答案
#define N 588
#include <stdlib.h>
#include <time.h>
void gen(char *dst)
{
int i, n;
srand(time(NULL)); /* init seed */
if ((dst = malloc(N)) == NULL) /* caller will need to free this */
return;
for (i = 0; i < N; )
if ((n = rand()) < 'A' && n > 'Z')
dst[i++] = n;
dst[N - 1] = 0; /* null terminate the string */
}
关于C生成最大长度的随机字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33464816/