C语言生成随机字符串(代码实例)
程序员文章站
2022-12-10 21:51:00
C语言生成随机字符串(代码实例)
// C语言生成随机字符.
// VS2017 15.6.4
#include
#...
C语言生成随机字符串(代码实例)
// C语言生成随机字符. // VS2017 15.6.4 #include <stdio.h> #include <stdlib.h> #include <time.h> const unsigned char allChar[63] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; void generateString(unsigned char * dest, unsigned int len); int main() { const unsigned int strlen = 20; unsigned char str[strlen + 1] = {}; generateString(str, strlen); printf("%s\n", str); return 0; } void generateString(unsigned char * dest, const unsigned int len) { unsigned int cnt, randNo; srand((unsigned int)time(NULL)); for (cnt = 0; cnt<len; cnt++) { randNo = rand() % 62; *dest = allChar[randNo]; dest++; } *dest = '\0'; }
上一篇: 一张图让你看懂数据结构、专业术语和概念
下一篇: 六种免费搭建自己的在线视频播放站的方法