写一函数随机生成一字符串,该字符串的长度为0~20,字符串为字母。请写出该代码。

2025-05-06 08:40:22
推荐回答(3个)
回答1:

#include
void main()
{
int i;
char *p="abcdealsdkjflsdfgdsafgdfasdgasdfgdafgb";
for(i=0;i<20;i++)
{
printf("%c",*p);
p++;
}
printf("\n");
}

回答2:

This is VB 6 solution, you can convert it to you language!

public function rndString$()
dim strBase$, str$, i#, strLen#
Randomize Timer()
strBase="abcdefghijklmnopqrstuvwxyz"
strLen=Fix(21*Rnd())
if strLen =0 then
str=""
else
for i=1 to strLen
str=str & Mid(strBase,Fix(26*Rnd())+1,1)
next
end if
end function

回答3:

final char ACHAR = 'a';
Random random = new Random();
int strLength = random.nextInt(20);
String result = "";
for (int i=0;i int charIncrease = random.nextInt(25);
result += (char)(ACHAR + charIncrease);
}
return result;