回到技术博客

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

//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;
    }
}
— 写于生活,留给未来 —

最后更新于 2024-11-17

树下留言

LET’S TALK

文字是一次相遇。很高兴听到你的声音。

评论加载中…