C++随机字符串生成函数

//C++ 随机字符串生成函数 #include #include using namespace std; const int LEN_NAME=4; char *rand_str(char *str,const int len) { int...

2020年10月25日
45字
5 阅读



//C++ 随机字符串生成函数
#include<iostream>
#include<ctime>
using namespace std;
 
const int LEN_NAME=4;
 
char *rand_str(char *str,const int len)
{
    int i;
    for(i=0;i<len;++i)
        str[i]='A'+rand()%26;
    str[++i]='\0';
    return str;
}
 
void main()
{
    srand(time(NULL));
    int i;
    char name[LEN_NAME+1];
 
    for(i=0;i<20;++i)
    {
        cout<<rand_str(name,LEN_NAME)<<endl;
    }
}


文章评论区

欢迎留言交流

未登录,请先注册或登录后发表评论。

Leave comment