C语言随机生成5000个字符串
时间: 2024-01-03 21:04:15 浏览: 185
随机产生字符串函数 c语言
4星 · 用户满意度95%
以下是C语言随机生成5000个字符串的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_LEN 100
char* generateRandomString() {
int len = rand() % MAX_LEN + 1; char* str = (char*)malloc(sizeof(char) * (len + 1));
for (int i = 0; i < len; i++) {
str[i] = rand() % 26 + 'a';
}
str[len] = '\0';
if (rand() % 2 == 0) {
int i = 0, j = len - 1;
while (i < j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
return str;
}
int main() {
srand(time(NULL));
for (int i = 0; i < 5000; i++) {
char* str = generateRandomString();
printf("%s\n", str);
free(str);
}
return 0;
}
```
阅读全文