华为测试题C语言版

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

例1.连续字符的统计#includestdio.h#includestring.hvoidmaxcommonchar(char*str,intlength);intmain(){char*a=assddff;intlen=strlen(a);maxcommonchar(a,len);return0;}voidmaxcommonchar(char*str,intlength){inti,n=1;for(i=0;ilength;i++){if(str[i]==str[i+1]){n++;}else{printf(%c:%d,str[i],n);n=1;}}}运行结果:a:1s:2d:2f:2例2统计字符串中连续字符最长的字符串并输出#includestdio.h#includestring.hvoidmaxcommonchar(char*str,intlength);intmain(){char*a=aabbccddddef;intlen=strlen(a);maxcommonchar(a,len);return0;}voidmaxcommonchar(char*str,intlength){inti,n=1,max=0;charc;for(i=0;ilength;i++){if(str[i]==str[i+1])n++;elseif(nmax){max=n;n=1;c=str[i];}}printf(最长连续字符串是%c:%d,c,max);printf(\n);}输出结果:最长连续字符串是:d:4例3判断一个字符串中()是否配对#includestdio.h#includestring.hboolmatch(char*str,intlength);intmain(){charb[100];char*s=b;intlen;boolm;s=90d(jk)jk(ghy(23)ss);len=strlen(s);m=match(s,len);if(m)printf(match!\n);elseprintf(nomatch!\n);return0;}boolmatch(char*str,intlength){inti,count1=0,count2=0;for(i=0;ilength;i++){if(*(str+i)=='(')count1++;if(*(str+i)==')')count2++;if(count2count1)returnfalse;}if(count1==count2)returntrue;elsereturnfalse;}输出结果:match!例3查找子字符串个数#includestdio.h#includestring.hintfun(char*str1,char*str2);intmain(){chars1[100]=aabbssffbbcccceesbb;chars2[100]=bb;printf(子字符串个数是:%d\n,fun(s1,s2));return0;}intfun(char*str1,char*str2){intlen1=strlen(str1);intlen2=strlen(str2);inti,count=0;for(i=0;ilen1-len2+1;i++){if(strncmp(str1+i,str2,len2)==0)count++;}returncount;}输出的结果是:子字符串个数是:3例4简单的字符统计#includestdio.h#includestring.h#includemalloc.hintmain(){intlen,i,a[128]={0},p;char*str=(char*)malloc(sizeof(char)*40);gets(str);len=strlen(str);for(i=0;ilen;i++){p=str[i];a[p]++;}for(i=1;i128;i++){if(a[i]!=0){printf(%c:%d,i,a[i]);}}printf(\n);return0;}例5删除字符串中字符数最少的字符#includestdio.h#includestring.hintmain(){char*str=affddsass;intlen=strlen(str);inta[128]={0};intp,min,i,k=0;charb[128];for(i=0;ilen;i++){p=str[i];a[p]++;}i=0;while(a[i]==0)i++;min=a[i];for(i=0;i128;i++){if(a[i]!=0&&a[i]min){min=a[i];}}for(i=0;str[i]!='\0';i++){if(a[str[i]]!=min){b[k++]=str[i];}}b[k]='\0';for(i=0;b[i]!='\0';i++){putchar(b[i]);}putchar('\n');return0;}输出结果:sss例5用指针求出字符串长度#includestdio.h#includestring.hintmain(){intlength=0;charstr[50];char*pstr=str;printf(Pleaseinputastring:\n);gets(str);while(*pstr++)length++;printf(字符串的长度:%d\n,length);return0;}例6字符串中子字符串的替换#includestdio.h#includestring.hchar*strReplace(chars1[],chars2[],chars3[]);intmain(){chars1[]=abcdefbbbsddsbbb;chars2[]=bbb;chars3[]=ggg;char*ps=strReplace(s1,s2,s3);puts(ps);return0;}char*strReplace(chars1[],chars2[],chars3[]){inti;intlen1=strlen(s1);intlen2=strlen(s2);intlen3=strlen(s3);for(i=0;i=len1-len2;i++){if(strncmp(s1+i,s2,len2)==0){strncpy(s1+i,s3,len3);}}returns1;}输出结果:abcdefgggsddsggg例7字符串间隔输出//编写一个程序实现功能:将字符串“ComputerSecience”付给一个字符数组,然后从第一//个字母开始间隔的输出该串,用指针完成#includestdio.h#includestring.hintmain(){intflag=1;charstr[]=ComputerSecience;char*pstr=str;while(*pstr){if(flag)putchar(*pstr);flag=(flag+1)%2;pstr++;}printf(\n);return0;}输出结果:Cmueeine例8两个字符串合并//编写一个程序实现一个功能:将两个字符串合并为一个字符串并且输出,用指针实现#includestdio.h#includestring.hintmain(){charstr1[20]=hello;charstr2[10]=world;char*pstr1=str1;char*pstr2=str2;while(*pstr1)pstr1++;while(*pstr2){*pstr1=*pstr2;pstr1++;pstr2++;}*pstr1='\0';puts(str1);return0;}输出结果:helloworld例算分数的问题//算分数问题,去掉一个最高分一个最低分,求平均分#includestdio.h#includestring.hfloatavescore(floatscore[],intlen);intmain(){floatscore[6]={70,80,90,98,87,86};floatlastscore;lastscore=avescore(score,6);printf(平均分是%f\n,lastscore);return0;}floatavescore(floatscore[],intlen){inti;floatmin=score[0];floatmax=score[0];floatsum=0.0,aver;for(i=1;ilen;i++){if(score[i]min)min=score[i];elseif(score[i]max)max=score[i];}for(i=0;ilen;i++){sum+=score[i];}sum=sum-min-max;aver=sum/(len-2);returnaver;}输出结果:85.750000例随机数按计数输出#includestdio.h#includestring.hvoidarray_iterate(intinput_array[],intlen,intm,intoutput_array[]);intmain(){constintlen=4;intinput_array[len]={3,1,2,4};intm=7,i;intoutput_array[len];array_iterate(input_array,len,m,output_array);for(i=0;ilen;i++){printf(%d,output_array[i]);}return0;}voidarray_iterate(intinput_array[],intlen,intm,intoutput_array[]){intk=0;intn=len;inti,j=-1;while(n){for(i=1;i=m;i++){j++;if(j==len)j=0;while(input_array[j]==0){j++;if(j==len)j=0;}}m=input_array[j];output_array[k++]=m;input_array[j]=0;n--;}}输入:input_array[len]={3,1,2,4};len=4;m=7输出:2314例统计一个数二进制表达式中0的个数#includestdio.hintfun(inta);intmain(){intcount;inta=10;count=fun(a);printf(%d中二进制表达式中0的个数是:%d\n,a,count);return0;}intfun(inta){intcount=0;while(a){if((a&1)==0)count++;a=(a1);}returncount;}输出结果:10中二进制表达式中0的个数是:2例镜像翻转二进制表达式,并输出十进制值#includestdio.hvoidfun(inta);intmain(){inta=10;fun(a);return0;}voidfun(inta){intb[30];inti=0,j;intsum=0;while(a){b[i++]=(a&1);a=(a1);}for(j=0;ji;j++){sum=sum*2+b[j];}printf(翻转后的二进制是:);for(j=0;ji;j++){printf(%d,b[j]);}printf(\n);printf(对应的十进制值是:%d\n,sum);}输出结果:翻转后的二进制是:0101对应的十进制值是:5例对数组从新一左一右排序#includestdio.h#includestring.hvoidsort(intinput_num[],intlen,intoutput_num[]);

1 / 16
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功