2022年華為實習工程師面試題(求職面試回答資料)_第1頁
2022年華為實習工程師面試題(求職面試回答資料)_第2頁
2022年華為實習工程師面試題(求職面試回答資料)_第3頁
2022年華為實習工程師面試題(求職面試回答資料)_第4頁
2022年華為實習工程師面試題(求職面試回答資料)_第5頁
已閱讀5頁,還剩85頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、 2022年華為實習工程師面試題第1題: 刪除子串,只要是原串中有相同的子串就刪掉,不管有多少個,返回子串個數(shù)。 #includestdio.h #includestdlib.h #includeassert.h #includestring.h intdelete_sub_str(constchar*str,constchar*sub_str,char*result) assert(str!=NULLsub_str!=NULL); constchar*p,*q; char*t,*temp; p=str; q=sub_str; t=result; intn,count=0; n=strlen(

2、q); tmep=(char*)malloc(n+1); memset(temp,0 x00,n+1); while(*p) memcpy(temp,p,n); if(strcmp(temp,q)=0) count+; memset(temp;0 x00,n+1); p=p+n; else *t=*p; p+; t+; memset(temp,0 x00,n+1); free(temp); returncount; intmain() chars100=0; intnum=delete_sub_str(“123abc12de234fg1hi34j123k”,”123”,s); printf(“

3、Thenumberofsub_stris%drn”,num); printf(“Theresultstringis%srn”,s); 第2題: 約瑟夫環(huán)是一個數(shù)學的應用問題:已知n個人(以編號1,2,3.n分別表示)圍坐在一張圓桌四周。從編號為k的人開頭報數(shù),數(shù)到m的那個人出列;他的下一個人又從1開頭報數(shù),數(shù)到m的那個人又出列;依此規(guī)律重復下去,直到圓桌四周的人全部出列。 #includestdio.h #includestdlib.h typedefstructNode intnum; structNode*next; LinkList; LinkList*creat(intn) LinkL

4、ist*p,*q,*head; inti=1; p=(LinkList*)malloc(sizeof(LinkList); p-num=i; head=p; for(i=2;i=n;i+) q=(LinkList*)malloc(sizeof(LinkList); q-num=i; p-next=q; p=q; p-next=head;/*使鏈表尾指向鏈表頭形成循環(huán)鏈表*/ returnhead; voidfun(LinkList*L,intm) inti; LinkList*p,*s,*q; p=L; printf(出列挨次為:); while(p-next!=p) for(i=1;im;i

5、+) q=p; p=p-next; printf(%5d,p-num); s=p; q-next=p-next; p=p-next; free(s); printf(%5dn,p-num); intmain() LinkList*L; intn,m; n=9; m=5; L=creat(n); fun(L,m); return0; 第3題: 比較一個數(shù)組的元素是否為回文數(shù)組 #includestdio.h #includestring.h inthuiwen(charstr) inti,len,k=1; len=strlen(str); for(i=0;ilen/2;i+) if(stri!=

6、strlen-i-1) k=1; break; if(k=0) printf(%s不是一個回文數(shù)n,str); else printf(%s是一個回文數(shù)n,str); voidmain() charstr100=0; inti; intlen; printf(Inputastring:);/*提示輸入Inputastring:*/ scanf(%s,str);/*scan()函數(shù)輸入一個字符串:*/ huiwen(str); 第4題: 比較兩個數(shù)組,要求從數(shù)組最終一個元素開頭逐個元素向前比較,假如2個數(shù)組長度不等,則只比較較短長度數(shù)組個數(shù)元素。請編程實現(xiàn)上述比較,并返回比較中發(fā)覺的不相等元素的

7、個數(shù) 比如:數(shù)組1,3,5和數(shù)組77,21,1,3,5按題述要求比較,不相等元素個數(shù)為0數(shù)組1,3,5和數(shù)組77,21,1,3,5,7按題述要求比較,不相等元素個數(shù)為3?要求實現(xiàn)函數(shù):intarray_compare(intlen1,intarray1,intlen2,intarray2)【輸入】intlen1:輸入被比較數(shù)組1的元素個數(shù);intarray1:輸入被比較數(shù)組1;intlen2:輸入被比較數(shù)組2的元素個數(shù);intarray2:輸入被比較數(shù)組2;【輸出】無【返回】不相等元素的個數(shù),類型為int?示例1)輸入:intarray1=1,3,5,intlen1=3,intarray2=7

8、7,21,1,3,5,intlen2=5函數(shù)返回:02)輸入:intarray1=1,3,5,intlen1=3,intarray2=77,21,1,3,5,7,intlen2=6函數(shù)返回: #includestdlib.h #includestdio.h #includestring.h intarray_compare(intlen1,intarray1,intlen2,intarray2) intcount=0; for(;len1=0len2=0;len1-,len2-) if(array1len1-1=array2len2-1) count+; returncount; intmai

9、n() intresult=0; intarray1=1,3,5; intlen1=3; intarray2=77,12,1,3,5; intlen2=5; result=array_compare(len1,array1,len2,array2);/result=array_compare(len1,array1,len2,array2);不能這樣 /函數(shù)形參中永久只是傳得首地址,不能傳數(shù)組切記切記! printf(theresultis%d,result); 第5題: 輸入一個由隨機數(shù)組成的數(shù)列(數(shù)列中每個數(shù)均是大于0的整數(shù),長度已知),和初始計數(shù)值m。從數(shù)列首位置開頭計數(shù),計數(shù)到m后,將

10、數(shù)列該位置數(shù)值替換計數(shù)值m,并將數(shù)列該位置數(shù)值出列,然后從下一位置從新開頭計數(shù),直到數(shù)列全部數(shù)值出列為止。假如計數(shù)到達數(shù)列尾段,則返回數(shù)列首位置連續(xù)計數(shù)。請編程實現(xiàn)上述計數(shù)過程,同時輸出數(shù)值出列的挨次 比如:輸入的隨機數(shù)列為:3,1,2,4,初始計數(shù)值m=7,從數(shù)列首位置開頭計數(shù)(數(shù)值3所在位置)第一輪計數(shù)出列數(shù)字為2,計數(shù)值更新m=2,出列后數(shù)列為3,1,4,從數(shù)值4所在位置從新開頭計數(shù)其次輪計數(shù)出列數(shù)字為3,計數(shù)值更新m=3,出列后數(shù)列為1,4,從數(shù)值1所在位置開頭計數(shù)第三輪計數(shù)出列數(shù)字為1,計數(shù)值更新m=1,出列后數(shù)列為4,從數(shù)值4所在位置開頭計數(shù)最終一輪計數(shù)出列數(shù)字為4,計數(shù)過程完成。

11、輸出數(shù)值出列挨次為:2,3,1,4。?要求實現(xiàn)函數(shù):voidarray_iterate(intlen,intinput_array,intm,intoutput_array)【輸入】intlen:輸入數(shù)列的長度;intintput_array:輸入的初始數(shù)列intm:初始計數(shù)值【輸出】intoutput_array:輸出的數(shù)值出列挨次【返回】無?示例輸入:intinput_array=3,1,2,4,intlen=4,m=7輸出:output_array=2,3,1,4 /循環(huán)鏈表實現(xiàn)/ #includestdio.h #includestdlib.h #includestring.h typ

12、edefstructNode intnum; structnode*next; node; node*creat(intlen,intinput_array) node*h,*s,*p; inti; h=(node*)malloc(sizeof(node); h-num=input_array0; p=h; for(i=1;ilen;i+) s=(node*)malloc(sizeof(node); s-num=input_arrayi; p-next=s; p=s; p-next=h; return(h); voidarray_iterate(intlen,intinput_array,in

13、tm) node*q,*p,*s; inti=0,j=0,k; intoutput_array4; p=creat(len,input_array); while(p-next!=p) for(i=1;im;i+) q=p; p=p-next; m=p-num; printf(%5d,m); output_arrayj+=m; s=p; q-next=p-next; p=p-next; free(s); s=NULL; m=p-num; printf(%5dn,m); output_arrayj=p-num; k=j; for(j=0;j=k;j+) printf(%5d,output_arr

14、ayj); intmain() intinput_array=3,1,2,4; intlen=4; intm=7; intoutput_array4; array_iterate(len,input_array,m,output_array); 第6題: 我國大陸運營商的手機號碼標準格式為:國家碼+手機號碼,例如:8613912345678。特點如下: 1、長度13位; 2、以86的國家碼打頭; 3、手機號碼的每一位都是數(shù)字。 請實現(xiàn)手機號碼合法性推斷的函數(shù)要求: 1)假如手機號碼合法,返回0; 2)假如手機號碼長度不合法,返回1 3)假如手機號碼中包含非數(shù)字的字符,返回2; 4)假如手機號碼

15、不是以86打頭的,返回3; 【注】除勝利的狀況外,以上其他合法性推斷的優(yōu)先級依次降低。也就是說,假如推斷出長度不合法,直接返回1即可,不需要再做其他合法性推斷。 l要求實現(xiàn)函數(shù): intverifyMsisdn(char*inMsisdn) 【輸入】char*inMsisdn,表示輸入的手機號碼字符串。 【輸出】無 【返回】推斷的結(jié)果,類型為int。 l示例 輸入:inMsisdn=“869123456789“ 輸出:無 返回:1 輸入:inMsisdn=“88139123456789“ 輸出:無 返回:3 輸入:inMsisdn=“86139123456789“ 輸出:無 返回:0 #inc

16、ludestdio.h #includestdlib.h #includeassert.h #includestring.h #defineLENGTH13 intverifyMsisdn(char*inMsisdn) char*pchar=NULL; assert(inMsisdn!=NULL); if(LENGTH=strlen(inMsisdn) if(8=*inMsisdn)(*(inMsisdn+1)=6) while(*inMsisdn!=0) if(*inMsisdn=0)(*inMsisdn=9) inMsisdn+; else return2; else return3; e

17、lse return1; return0; intmain() char*pchar=NULL; unsignedcharichar=0; intresult; switch(ichar) case0: pchar=8612345363789;break; case1: pchar=861111111111111;break; case2: pchar=86s1234536366;break; default: break; result=verifyMsisdn(pchar); printf(resultis%dn,result); 第7題: 比較兩個數(shù)組,要求從數(shù)組最終一個元素開頭逐個元素

18、向前比較,假如2個數(shù)組長度不等,則只比較較短長度數(shù)組個數(shù)元素。請編程實現(xiàn)上述比較,并返回比較中發(fā)覺的不相等元素的個數(shù) 比如:數(shù)組1,3,5和數(shù)組77,21,1,3,5按題述要求比較,不相等元素個數(shù)為0數(shù)組1,3,5和數(shù)組77,21,1,3,5,7按題述要求比較,不相等元素個數(shù)為3?要求實現(xiàn)函數(shù):intarray_compare(intlen1,intarray1,intlen2,intarray2)【輸入】intlen1:輸入被比較數(shù)組1的元素個數(shù);intarray1:輸入被比較數(shù)組1;intlen2:輸入被比較數(shù)組2的元素個數(shù);intarray2:輸入被比較數(shù)組2;【輸出】無【返回】不相等元

19、素的個數(shù),類型為int?示例1)輸入:intarray1=1,3,5,intlen1=3,intarray2=77,21,1,3,5,intlen2=5函數(shù)返回:02)輸入:intarray1=1,3,5,intlen1=3,intarray2=77,21,1,3,5,7,intlen2=6函數(shù)返回:3 #includestdlib.h #includestdio.h #includestring.h intarray_compare(intlen1,intarray1,intlen2,intarray2) intcount=0; for(;len1=0len2=0;len1-,len2-)

20、if(array1len1-1=array2len2-1) count+; returncount; intmain() intresult=0; intarray1=1,3,5; intlen1=3; intarray2=77,12,1,3,5; intlen2=5; result=array_compare(len1,array1,len2,array2); /result=array_compare(len1,array1,len2,array2);不能這樣 /函數(shù)形參中永久只是傳得首地址,不能傳數(shù)組切記切記! printf(theresultis%d,result); 第8題: 問題描

21、述:輸入一個只包含個位數(shù)字的簡潔四則運算表達式字符串,計算該表達式的值 注:1、表達式只含+,-,*,/四則運算符,不含括號2、表達式數(shù)值只包含個位整數(shù)(0-9),且不會消失0作為除數(shù)的狀況3、要考慮加減乘除按通常四則運算規(guī)定的計算優(yōu)先級4、除法用整數(shù)除法,即僅保留除法運算結(jié)果的整數(shù)部分。比如8/3=2。輸入表達式保證無0作為除數(shù)狀況發(fā)生5、輸入字符串肯定是符合題意合法的表達式,其中只包括數(shù)字字符和四則運算符字符,除此之外不含其它任何字符,不會消失計算溢出狀況?要求實現(xiàn)函數(shù):intcalculate(intlen,char*expStr)【輸入】intlen:字符串長度;char*expStr

22、:表達式字符串;【輸出】無【返回】計算結(jié)果?示例1)輸入:char*expStr=“1+4*5-8/3”函數(shù)返回:192)輸入:char*expStr=“8/3*3”函數(shù)返回:6 #includestdio.h /* *authorbywanww *time:2022-09-07 */ usingnamespacestd; intarray_compare(intlen1,intarray1,intlen2,intarray2) if(len1=len2) intcount=0; for(inti=0;ilen1;i+) if(array1i!=array2i)count+; returnco

23、unt; elseif(len1len2) returnarray_compare(len1,array1,len1,array2+len2-len1); else returnarray_compare(len2,array1+len1-len2,len2,array2); voidarray_iterate(intlen,intinput_array,intm,intoutput_array) int*flag=newintlen; memset(flag,0,len*4); inthasout=0;/已經(jīng)出列的數(shù)字個數(shù) intstart=0;/開頭的下標號 intj=0; /當前以報到的

24、數(shù)字 while(true) if(flagstart=0) /當前元素還沒出列 j+; if(j=m) /已經(jīng)計數(shù)到m,當前start下標的元素出列 output_arrayhasout=input_arraystart; flagstart=1; /標記當前元素已經(jīng)出列 hasout+; if(hasout=len)break;/全部的元素都已經(jīng)出列,結(jié)束程序 /初始化下一輪的數(shù)字 j=0; m=input_arraystart; start+; if(start=len)start=0; deleteflag; intcalculate(intlen,char*expStr) struc

25、t charopdata200; inttop; opstack; /定義操作符棧 opstack.top=-1; inti=0;/遍歷字符串的下標 intt=0;/當前后綴表達式的長度 charch=expStri; while(ch!=0) switch(ch) case+: case-: while(opstack.top!=-1) expStrt=opstack.opdataopstack.top; opstack.top-; t+; opstack.top+; opstack.opdataopstack.top=ch; break; case*: case/: while(opsta

26、ck.top!=-1(opstack.opdataopstack.top=*|opstack.opdataopstack.top=/) expStrt=opstack.opdataopstack.top; opstack.top-; t+; opstack.top+; opstack.opdataopstack.top=ch; break; default: expStrt=ch; t+; break; i+; ch=expStri; while(opstack.top!=-1)/將棧中全部的剩余的運算符出棧 expStrt=opstack.opdataopstack.top; opstack

27、.top-; t+; expStrt=0; struct intnumeric200; inttop; data; data.top=-1; i=0; ch=expStri; while(ch!=0) if(ch=0ch=9) data.top+; data.numericdata.top=ch-0; elseif(+=ch) inttmp=data.numericdata.top-1+data.numericdata.top; data.top-; data.numericdata.top=tmp; elseif(-=ch) inttmp=data.numericdata.top-1-dat

28、a.numericdata.top; data.top-; data.numericdata.top=tmp; elseif(*=ch) inttmp=data.numericdata.top-1*data.numericdata.top; data.top-; data.numericdata.top=tmp; elseif(/=ch) if(data.numericdata.top=0) printf(cannotbezeroofthedividen); exit(1); inttmp=data.numericdata.top-1/data.numericdata.top; data.to

29、p-; data.numericdata.top=tmp; i+; ch=expStri; returndata.numericdata.top; voidmain() intarray1=1,3,5; intlen1=3; intarray2=77,21,1,3,5,7; intlen2=6; intcount= array_compare(sizeof(array1)/sizeof(int),array1,sizeof(array2)/sizeof(int),array2); printf(%dn,count); printf(*n); intinput_array=3,1,2,4; in

30、tlen=4; intm=7; int*output_array=newintsizeof(input_array)/sizeof(int); array_iterate(4,input_array,7,output_array); for(inti=0;isizeof(input_array)/sizeof(int);i+) printf(%d,output_arrayi); deleteoutput_array; printf(n*n); charexpStr=8/3*3; intresult=calculate(strlen(expStr),expStr); printf(%sn,exp

31、Str); printf(%dn,result); 第9題: 選秀節(jié)目打分,分為專家評委和大眾評委,score數(shù)組里面存儲每個評委打的分數(shù),judge_type里存儲與score數(shù)組對應的評委類別,judge_typei=1,表示專家評委,judge_typei=2,表示大眾評委,n表示評委總數(shù)。打分規(guī)章如下:專家評委和大眾評委的分數(shù)先分別取一個平均分(平均分取整),然后,總分=專家評委平均分*0.6+大眾評委*0.4,總分取整。假如沒有大眾評委,則總分=專家評委平均分,總分取整。函數(shù)最終返回選手得分。 函數(shù)接口intcal_score(intscore,intjudge_type,intn)

32、 #includestdio.h #includestring.h #includeiostream.h #includeconio.h #defineN5 intcal_score(intscore,intjudge_type,intn) intexpert=0; intdazhong=0; intzongfen=0; inti; intnumber=0; for(i=0;iN;i+) if(judge_typei=1) expert=expert+scorei; number+; elsedazhong=dazhong+scorei; if(number=N) zongfen=(int)(

33、expert/N); else expert=(int)(expert/number); dazhong=(int)(dazhong/(N-number); zongfen=int(0.6*expert+0.4*dazhong); returnzongfen; intmain() intscoreN; intjudge_typeN; intnumberlast=0; inti; printf(pleaseinputthe%dscore:n,N); for(i=0;iN;i+) scanf(%d,scorei); printf(pleaseinputthelevel(1:expert,2:daz

34、hong)n); for(i=0;iN;i+) scanf(%d,judge_typei); numberlast=cal_score(score,judge_type,N); printf(thelastscoreis%dn,numberlast); return0; 第10題: 給定一個數(shù)組input,假如數(shù)組長度n為奇數(shù),則將數(shù)組中最大的元素放到output數(shù)組最中間的位置,假如數(shù)組長度n為偶數(shù),則將數(shù)組中最大的元素放到output數(shù)組中間兩個位置偏右的那個位置上,然后再按從大到小的挨次,依次在第一個位置的兩邊,根據(jù)一左一右的挨次,依次存放剩下的數(shù)。 例如:input=3,6,1,9,7

35、output=3,7,9,6,1;input=3,6,1,9,7,8output=1,6,8,9,7,3 #includestdio.h #includestring.h #includeconio.h voidsort(intinput,intn,intoutput) inti,j; intk=1; inttemp; intmed; for(i=0;in;i+) for(j=0;jn-i;j+) if(inputjinputj+1) temp=inputj;inputj=inputj+1;inputj+1=temp; if(n%2!=0) for(i=0;in;i+) printf(%2d,

36、inputi); printf(n); med=(n-1)/2; outputmed=inputn-1; for(i=1;i=med;i+) outputmed-i=inputn-1-k; outputmed+i=inputn-2-k; k=k+2; else for(i=0;in;i+) printf(%2d,inputi); printf(n); med=n/2; outputmed=inputn-1; for(i=1;i=med-1;i+) outputmed-i=inputn-1-k; outputmed+i=inputn-2-k; k=k+2; output0=input0; for

37、(i=0;in;i+) printf(%2d,outputi); printf(n); intmain() inta6=3,6,1,9,7,8; intb6=0; for(inti=0;i6;i+) printf(%2d,ai); printf(n); sort(a,6,b); return0; 第11題: 操作系統(tǒng)任務調(diào)度問題。操作系統(tǒng)任務分為系統(tǒng)任務和用戶任務兩種。其中,系統(tǒng)任務的優(yōu)先級50,用戶任務的優(yōu)先級=50且=255。優(yōu)先級大于255的為非法任務,應予以剔除?,F(xiàn)有一任務隊列task,長度為n,task中的元素值表示任務的優(yōu)先級,數(shù)值越小,優(yōu)先級越高。函數(shù)scheduler實現(xiàn)如下功

38、能,將task中的任務根據(jù)系統(tǒng)任務、用戶任務依次存放到system_task數(shù)組和user_task數(shù)組中(數(shù)組中元素的值是任務在task數(shù)組中的下標),并且優(yōu)先級高的任務排在前面,數(shù)組元素為-1表示結(jié)束。 例如:task=0,30,155,1,80,300,170,40,99system_task=0,3,1,7,-1user_task=4,8,2,6,-1 函數(shù)接口voidscheduler(inttask,intn,intsystem_task,intuser_task) #includestdio.h #includestring.h #includemalloc.h #include

39、iostream.h voidscheduler1(inttask,intn,intsystem_task,intuser_task) inti; intj=0; int*p,*pp,*p_user,*pp_user; intindex=0; intcount,count2; intmin=0; intk=0; p=(int*)malloc(sizeof(int)*n); for(i=0;in;i+) pi=0; pp=(int*)malloc(sizeof(int)*n); for(i=0;in;i+) ppi=0; p_user=(int*)malloc(sizeof(int)*n); f

40、or(i=0;in;i+) p_useri=0; pp_user=(int*)malloc(sizeof(int)*n); for(i=0;in;i+) pp_useri=0; for(i=0;in;i+) if(taski50) system_taskj=taski; ppj=i; j+; count=j; elseif(taski=255) user_taskk=taski; pp_userk=i; k+; count2=k; elsetaski=taski; for(i=0;icount;i+) printf(%3d,system_taski); printf(n); for(i=0;i

41、count;i+) min=system_task0; for(j=1;jcount;j+) if(system_taskjmin) min=system_taskj; pi=j; system_taskpi=51; ppcount=-1; for(i=0;icount;i+) printf(%3d,pppi); printf(%3dn,ppcount); /*/ for(i=0;icount2;i+) printf(%4d,user_taski); printf(n); for(i=0;icount2;i+) min=user_task0; for(j=1;jcount2;j+) if(us

42、er_taskjmin) min=user_taskj; p_useri=j; user_taskp_useri=256; pp_usercount2=-1; for(i=0;icount2;i+) printf(%4d,pp_userp_useri); printf(%3dn,pp_usercount2); intmain() inttask9=0,30,155,1,80,300,170,40,99; intsystem_task9=0; intuser_task9=0; scheduler1(task,9,system_task,user_task); return0; 第12題: 從兩個

43、數(shù)組的最終一個元素比較兩個數(shù)組中不同元素的個數(shù),如有array15=77,21,1,3,5,array23=1,3,5,從array14與array22比較開頭,到array12與array0比較結(jié)束。這樣得出它們不同的元素個數(shù)為0,若array16=77,21,1,3,5,7,那么他們不同的元素為3。 函數(shù)原型為intcompare_array(intlen1,intarray1,intlen2,intarray2); 其中,len1與len2分別為數(shù)組array1和array2的長度,函數(shù)返回值為兩個數(shù)組不同元素的個數(shù)。 以下是上題的函數(shù)完整實現(xiàn): /diff_num.cpp #inclu

44、destdio.h intcompare_array(intlen1,intarray1,intlen2,intarray2) inti,t,small,num=0; /把兩數(shù)組倒置 for(i=0;ilen1/2;i+) t=array1i; array1i=array1len1-i-1; array1len1-i-1=t; for(i=0;ilen2/2;i+) t=array2i; array2i=array2len2-i-1; array2len2-i-1=t; /輸出倒置后的兩數(shù)組 /* for(i=0;ilen1;i+) printf(%d,array1i); printf(n);

45、 for(i=0;ilen2;i+) printf(%d,array2i); */ printf(n); if(len1len2) small=len2; else small=len1; num=small; for(i=0;ismall;i+) if(array1i=array2i) num-; printf(num=%dn,num); returnnum; voidmain() intarray15=77,21,1,3,5,array23=1,3,5; intlen1=5,len2=3; compare_array(len1,array1,len2,array2); 第13題: 輸入一個

46、字符串,用指針求出字符串的長度。 #includestdio.h intmain() charstr20,*p; intlength=0; printf(“Pleaseinputastring:”); gets(str); p=str; while(*p+) length+; printf(“Thelengthofstringis%dn”,length); return0; 第14題: 使用C語言實現(xiàn)字符串中子字符串的替換 描述:編寫一個字符串替換函數(shù),如函數(shù)名為StrReplace(char*strSrc,char*strFind,char*strReplace),strSrc為原字符串,s

47、trFind是待替換的字符串,strReplace為替換字符串。 舉個直觀的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”這個字符串,把其中的“RST”替換為“ggg”這個字符串,結(jié)果就變成了: ABCDEFGHIJKLMNOPQgggUVWXYZ 答案一: #includestdio.h #includestring.h voidStrReplace(char*strSrc,char*strFind,char*strReplace); #defineM100; voidmain() chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ; chars1=RST

48、; chars2=ggg; StrReplace(s,s1,s2); printf(%sn,s); voidStrReplace(char*strSrc,char*strFind,char*strReplace) inti=0; intj; intn=strlen(strSrc); intk=strlen(strFind); for(i=0;in;i+) if(*(strSrc+i)=*strFind) for(j=0;jk;j+) if(*(strSrc+i+j)=*(strFind+j) *(strSrc+i+j)=*(strReplace+j); elsecontinue; 答案二: #

49、includestdio.h #defineMAX100 StrReplace(char*s,char*s1,char*s2) char*p; for(;*s;s+) for(p=s1;*p*p!=*s;p+); if(*p)*s=*(p-s1+s2); intmain() charsMAX;/s是原字符串 chars1MAX,s2MAX;/s1是要替換的 /s2是替換字符串 puts(Pleaseinputthestringfors:); scanf(%s,s); puts(Pleaseinputthestringfors1:); scanf(%s,s1); puts(Pleaseinput

50、thestringfors2:); scanf(%s,s2); StrReplace(s,s1,s2); puts(Thestringofsafterdisplaceis:); printf(%sn,s); return0; 答案三: #includestdio.h #includestdlib.h #includestring.h #defineM100 voidStrReplace(char*strSrc,char*strFind,char*strReplace); intmain() chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ; chars1=RST; chars2

51、=gggg; StrReplace(s,s1,s2); printf(%sn,s); return0; voidStrReplace(char*strSrc,char*strFind,char*strReplace) while(*strSrc!=0) if(*strSrc=*strFind) if(strncmp(strSrc,strFind,strlen(strFind)=0) inti=strlen(strFind); intj=strlen(strReplace); printf(i=%d,j=%dn,i,j); char*q=strSrc+i; printf(*q=%sn,q); w

52、hile(*strSrc+=*strReplace+)!=0); printf(strSrc-1=%sn,strSrc-1); printf(*q=%sn,q); while(*strSrc+=*q+)!=0); else strSrc+; else strSrc+; 第15題: 編寫一個程序?qū)崿F(xiàn)功能:將字符串”ComputerSecience”賦給一個字符數(shù)組,然后從第一個字母開頭間隔的輸出該串,用指針完成。 #includestdio.h #includestring.h intmain() charstr=”ComputerScience”; intflag=1; char*p=str;

53、 while(*p) if(flag) printf(“%c”,*p); flag=(flag+1)%2; p+; printf(“n”); return0; 第16題: 使用C語言實現(xiàn)字符串中子字符串的替換 描述:編寫一個字符串替換函數(shù),如函數(shù)名為StrReplace(char*strSrc,char*strFind,char*strReplace),strSrc為原字符串,strFind是待替換的字符串,strReplace為替換字符串。 舉個直觀的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”這個字符串,把其中的“RST”替換為“ggg”這個字符串,結(jié)果就變成了:

54、ABCDEFGHIJKLMNOPQgggUVWXYZ 答案一: #includestdio.h #includestring.h voidStrReplace(char*strSrc,char*strFind,char*strReplace); #defineM100; voidmain() chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ; chars1=RST; chars2=ggg; StrReplace(s,s1,s2); printf(%sn,s); voidStrReplace(char*strSrc,char*strFind,char*strReplace) in

55、ti=0; intj; intn=strlen(strSrc); intk=strlen(strFind); for(i=0;in;i+) if(*(strSrc+i)=*strFind) for(j=0;jk;j+) if(*(strSrc+i+j)=*(strFind+j) *(strSrc+i+j)=*(strReplace+j); elsecontinue; 答案二: #includestdio.h #defineMAX100 StrReplace(char*s,char*s1,char*s2) char*p; for(;*s;s+) for(p=s1;*p*p!=*s;p+); if

56、(*p)*s=*(p-s1+s2); intmain() charsMAX;/s是原字符串 chars1MAX,s2MAX;/s1是要替換的 /s2是替換字符串 puts(Pleaseinputthestringfors:); scanf(%s,s); puts(Pleaseinputthestringfors1:); scanf(%s,s1); puts(Pleaseinputthestringfors2:); scanf(%s,s2); StrReplace(s,s1,s2); puts(Thestringofsafterdisplaceis:); printf(%sn,s); retur

57、n0; 答案三: #includestdio.h #includestdlib.h #includestring.h #defineM100 voidStrReplace(char*strSrc,char*strFind,char*strReplace); intmain() chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ; chars1=RST; chars2=gggg; StrReplace(s,s1,s2); printf(%sn,s); return0; voidStrReplace(char*strSrc,char*strFind,char*strReplace)

58、while(*strSrc!=0) if(*strSrc=*strFind) if(strncmp(strSrc,strFind,strlen(strFind)=0) inti=strlen(strFind); intj=strlen(strReplace); printf(i=%d,j=%dn,i,j); char*q=strSrc+i; printf(*q=%sn,q); while(*strSrc+=*strReplace+)!=0); printf(strSrc-1=%sn,strSrc-1); printf(*q=%sn,q); while(*strSrc+=*q+)!=0); el

59、se strSrc+; else strSrc+; 第17題: 編寫一個程序?qū)崿F(xiàn)功能:將兩個字符串合并為一個字符串并且輸出,用指針實現(xiàn)。 charstr120=“Hello”,str220=“World”; #includestdio.h intmain() charstr120=“Hello”,str220=“World”; char*p=str1,*q=str2; while(*p)p+; while(*q) *p=*q; p+; q+; *p=0; printf(“%sn”,str1); return0; 第18題: 算分數(shù)的問題,去掉一個最高分一個最低分,求平均分 #includest

60、dio.h floatavescore(floatscore,intn) floatmin=0; floatmax=0; intminindex=0; intmaxindex=0; floatsum=0; min=score0; for(inti=0;in;i+) if(scoreimin) min=scorei; minindex=i; scoreminindex=0; max=score0; for(i=0;in;i+) if(scoreimax) max=scorei; maxindex=i; scoremaxindex=0; for(i=0;in;i+) sum+=scorei; su

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論