1. 程式人生 > >C語言隨機產生字串

C語言隨機產生字串

#include"stdafx.h"
#include <stdio.h>  
#include<time.h>
#include <Windows.h>  

//產生長度為length的隨機字串    
int genRandomString(int length, char* ouput)
{
	int flag, i;
	srand((unsigned)time(NULL));
	for (i = 0; i < length - 1; i++)
	{
		flag = rand() % 3;
		switch (flag)
		{
		case 0:
			ouput[i] = 'A' + rand() % 26;
			break;
		case 1:
			ouput[i] = 'a' + rand() % 26;
			break;
		case 2:
			ouput[i] = '0' + rand() % 10;
			break;
		default:
			ouput[i] = 'x';
			break;
		}
	}
	return 0;
}

int main()
{
	char line[20000] = { 0 };
	memset(line, 0, 2000);
	genRandomString(1000, line);
	printf("line=%s\n", line);
	memset(line, 0, 2000);
	Sleep(1000);
	genRandomString(899, line);
	printf("line=%s\n", line);
}

在這裡插入圖片描述
歡迎關注本人微信公眾號,更好技術和行業好文等您收看!