programming in ANSI C-Chapter 8 Character Arrays and Strings_第1页
programming in ANSI C-Chapter 8 Character Arrays and Strings_第2页
programming in ANSI C-Chapter 8 Character Arrays and Strings_第3页
programming in ANSI C-Chapter 8 Character Arrays and Strings_第4页
programming in ANSI C-Chapter 8 Character Arrays and Strings_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

.,Chapter8CharacterArraysandStrings,PROGRAMMINGINANSIC,.,CharacterArrays,Inacharacterarray,eachelementstoresonecharacter.e.g.charc5;c0=H;c1=e;c2=c3=l;c4=o;,c0=72;c1=101;c2=c3=108;c4=111;,.,CharacterArrays-Initialization,Likeothertypeone-dimensionalarrays,thecharacterarraycanbeinitializedinthesameway.e.g.charc5=H,e,l,l,o;Ifthenumberofinitializersislessthanthedeclaredsize,theremainingelementsareinitializedtonullcharacter(0).e.g.charc6=H,e,l,l,o;,.,CharacterArrays-Initialization,Likeothertypeone-dimensionalarrays,thecharacterarraycanbeinitializedinthesameway.e.g.charc5=H,e,l,l,o;Ifthenumberofinitializersismorethanthedeclaredsize,thecomplierwillproduceanerror.e.g.charc4=H,e,l,l,o;,Error:Toomanyinitializersinfunction.,.,CharacterArrays-Initialization,Likeothertypeone-dimensionalarrays,thecharacterarraycanbeinitializedinthesameway.e.g.charc5=H,e,l,l,o;Ifthenumberofinitializersisequaltothedeclaredsize,thesizemaybeomitted.e.g.charc=H,e,l,l,o;,.,CharacterArrays,Thecharacterarraycanbeusedasothertypeone-dimensionalarrays.,main()charc10=I,a,m,h,a,p,p,y;inti;for(i=0;i10;i+)printf(%c,ci);,Iamhappy,.,Strings,Astringisacharacterarray.charc10=I,a,m,h,a,p,p,y;Theactuallengthofthestringisnotalwaysequaltothesizeofthecharacterarray.Infact,weoftenconcerntheactuallengthofastringratherthanthesizeofacharacterarray.charc100=I,a,m,h,a,p,p,y;Thesizeofthecharacterarrayis100,buttheactuallengthofthestringisonly10.,.,Strings,Thearraysizeisoftenmuchlargerthanthesizeofthestringstoredinthearray.Inordertorepresenttheendofthestring,thenullcharacter0isusedastheend-of-stringmarker.charc5=B,o,y;,c3istheend-of-stringmarker0,sotheelementsbeforeitcomposethestring,andthelengthofthestringis3.,.,Strings,Payattentiontothedistinctionofthenullcharacter0,andtheblankspacecharacter,andthefigurecharacter0.TheASCIIvalueof0is0.Itcantbedisplayedandactsastheend-of-stringmarker.TheASCIIvalueofis32.Itcanbeoutputtedasablankandoccupyoneplace.TheASCIIvalueof0is48.Itcanbeoutputtedasafigure.,.,Strings,Whenastringconstantisstoredinmemory,italsousesthenullcharacter0astheend-of-stringmarker.Forexample:WhenthestringHelloisstoredinmemory,itrequires6bytesstorage,andthelastbyteisusedtostorethenullcharacter0.,.,Strings-Initialization,Usingcharacters.charc5=G,o,o,d;Usingstring.charc5=Good;charc5=Good;charc=Good;,Notice:Thearraycconsistsof5elementsbutnot4elements!Thisdeclarationisequivalentto:charc5=Good;Itdifferswithcharc4=Good;,.,Strings-Initialization,Theinitializationoftableofstrings.charfruit7=Apple,Orange,Grape,Pear,Peach;,.,InputandOutput%c,%c,main()charstr6;inti;for(i=0;i-1=aA=32=C0=67=ua=20=05=-53,.,String-HandlingFunctionsstrcmp(),strcmp(str1,str2)Wecantuse=operatortocomparetwostrings.while(st1=str2).changeto:while(strcmp(str1,str2)=0)orwhile(!strcmp(str1,str2),.,String-HandlingFunctionsstrlen(),strlen(str)Function:Returnthelengthofthestring(thenumberofthecharactersofstrexceptthenullcharacter).,Forthesefollowingdeclarations,whatisthevalueofthefunctionstrlen(s)?(1)chars10=A,0,B,C,0,D;(2)chars=tb0willn;(3)chars=x69082n;,1(A),3(tb),1(i),.,String-Program1,Writeaprogramtocalculatethelengthofastring.Dontusethefunctionstrlen().Step1:Declareacharacterarrays100;Step2:Readinastring.Step3:Calculatethenumberofthecharactersbeforethefirstnullcharacterins.Itisthelengthofs.Step4:Outputthevalueofthenumber.,.,String-Program1,main()chars100;inti=0,slen=0;printf(Pleasinputthestring:n);scanf(%s,s);while(si!=0)slen+;i+;printf(Thelengthofstringis:%dn,slen);,while(si!=0),while(si),Pleaseinputthestring:HelloThelengthofstringis5.,Pleaseinputthestring:Howareyou?Thelengthofstringis3.,.,String-Program2,Writeaprogramtoconcatenate2strings.Dontusethefunctionstrcat().Step1:Declaretwocharacterarraysa100,b50;Step2:Readtwostringsintoarraysaandb.Step3:Beginwiththenullcharacterofthearrayatocopyallthecharactersbeforethenullcharacterofarraybintoarraya.Step4:Outputthearraysaandb.,.,String-Program2,main()chara100,b50;inti=0,j=0;printf(Pleasinput2strings:n);scanf(%s%s,a,b);while(ai!=0)i+;while(bj!=0)ai=bj;i+;j+;ai=0;printf(a:%snb:%sn,a,b);,Pleaseinput2strings:Goodboy!a:Goodboy!b:boy!,.,String-Program3,Writeaprogramtoreversethecharactersofastring.Step1:Declareacharacterarraysa50;Step2:Readastringintoa.Step3:Getthesubscriptofthenullcharacterofthearraya,andassignittovariablei.Step4:for(j=0;ji;j+,i-)exchangeaiandaj.,.,String-Program3,main()chara30,ch;inti=0,j=0;printf(Pleasinputthestring:n);sc

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论