#include <time.h>
int main()
{
char str[62]="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char captcha[4];
char captchatest[4];
int num=4;
int a[4]={0};
int count=0;
srand((unsigned)time(NULL));
while(num!=0)
{
a[count++]=rand()%62;
num--;
}
while(count!=0)
{
count--;
captcha[count]=str[a[count]];
}
printf("%s\n",captcha);
}
- 在使用验证码生成时调用了随机数函数,并且结合字符串数组得到一个4位的验证码,但是在函数封装时产生了问题,不知是否为野指针的问题,造成结果不能反回。待项目解决后深入研究
char str[62]="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char captcha[8];
char captchatest[8];
int num=4;
int a[4]={0};
int count=0,i;
memset(captcha,0,8);
srand((unsigned)time(NULL));
while(num!=0)
{
a[count]=rand()%61;
count++;
num--;
}
count = 4;
while(count!=0)
{
count--;
captcha[count]=str[a[count]];
}
printf("验证码:%s\n",captcha);
printf("验证码:");
scanf("%s",captchatest);
使用了memset函数成功将后缀乱码消除