#include <iostream> #include <ctime>
using namespace std;
const int LEN = 62; // 26 + 26 + 10
char g_arrCharElem[LEN] = {'0', '1', '2',... 'A', 'B', 'C', ... 'a', 'b', 'c'};
int main()
{
int iLen;
cout << "输入要生成的字符串长度:/n";
do
{
cin >> iLen;
} while (iLen <= 0);
char* szStr = new char[iLen + 1];
szStr[iLen] = '/0';
srand((unsigned)time(0));
int iRand = 0;
for (int i = 0; i < iLen; ++i)
{
iRand = rand() % LEN; // iRand = 0 - 61
szStr[i] = g_arrCharElem[iRand];
}
cout << "生成的随机字符串为:/n";
cout << szStr << endl;
delete[] szStr;
return 0;
}
随机生成指定长度字符串
最新推荐文章于 2022-03-26 22:16:25 发布