C生成最大长度的随机字符串

标签 c string random ascii

<分区>

我想生成一个仅由小写 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/

相关文章:

bash - 为什么 Bash 的 $RANDOM 没有在某些机器上播种(?)?

c - 通用 MPLAB X、XC8 和 C。跨函数和源文件使用变量

c - 如何使用函数将字符附加到 C 中的字符串

c++ - 从字符串中删除空格 - 带指针的就地 C 样式

java - 比较 MD5 字符串

c++ - 有没有可以和 boost::uniform_int 媲美的 Go 函数?

用于 Julia 集群计算的随机数种子

c - 删除字符串末尾的路径分隔符

c++ - qsort() vs std::sort,比较函数哲学差异

c - 将字符串分配为结构成员时出现内存泄漏