首页 > 其他 > 详细

19.04.09小练习--随机+字符串

时间:2019-04-09 19:04:22      阅读:119      评论:0      收藏:0      [点我收藏+]

/*Linux平台vi编辑器*/

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<time.h>

//函数声明

int L_rand(void);/*返回一个0-9的随机值*/

int L_compare(char*,char*);/*比较字符串是否相同,相同返回1,不同返回0*/

//主函数

int main()

{

int nums[4];

char a1[]="abc",a2[]="abcde",a3[]="abced",a4[]="abc",a5[]="abcd";

srand((unsigned)time(NULL));//随机种子

for(int i=0;i<4;i++)nums[0]=L_rand();

printf("%d\t%d\t%d\t%d\n",nums[0],nums[1],nums[2],nums[3]);//显示四个随机的0-9数字

printf("%d\t%d\t%d\n",L_compare(a1,a5),L_compare(a2,a3),L_compare(a1,a4));//显示字符串1和字符串5,字符串2和字符串3,字符串1和字符串4是否相同

return 0;

}

//函数定义

int L_rand()

{

int a;

a=rand%9;

return a;

}

int L_compare(char *a,char *b)

{

int index=0,len1=strlen(a),len2=strlen(b),n=0;

while(a[index]==b[index]&&len1==len2)//如果两个字符串长度相同且index位上字符相同则index+1

{

index++;

if(a[index]==0)break;//直到字符串的结尾\0

}

if(a[index]==0)n=1;

returm n;

}

19.04.09小练习--随机+字符串

原文:https://www.cnblogs.com/redyellowblue/p/10678815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!