C语言学习第八章(英文版)

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

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

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

资源描述

Chapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C1Chapter8CharactersandStringsChapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C28.1Introduction8.2FundamentalsofStringsandCharacters8.3CharacterHandlingLibrary8.4StringConversionFunctions8.5StandardInput/OutputLibraryFunctions8.6StringManipulationFunctionsoftheStringHandlingLibrary8.7ComparisonFunctionsoftheStringHandlingLibrary8.8OtherFunctionsoftheStringHandlingLibrary8.9Two-DimensionalArrayofCharacters8.10ArrayofPointerstoStringsOutlineChapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C3FundamentalsofStringsandCharactersStandardInput/OutputLibraryFunctionsStringManipulationFunctionsoftheStringHandlingLibraryKeyPointsChapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C48.1IntroductionIntroducesomestandardlibraryfunctionsEasystringandcharacterprocessingProgramscanprocesscharacters,strings,linesoftext,andblocksofmemoryThesetechniquesusedtomakeWordprocessorsPagelayoutsoftwareTypesettingprogramsChapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C58.2FundamentalsofStringsandCharactersCharactersBuildingblocksofprogramsEveryprogramisasequenceofmeaningfullygroupedcharactersCharacterconstantAnintvaluerepresentedasacharacterinsinglequotes'z'representstheintegervalueofzStringsSeriesofcharacterstreatedasasingleunitCanincludeletters,digitsandspecialcharacters(*,/,$)Stringliteral(stringconstant)-writtenindoublequotesHelloStringsarearraysofcharactersStringapointertofirstcharacterValueofstringistheaddressoffirstcharacterChapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C68.2FundamentalsofStringsandCharactersStringdeclarationsDeclareasacharacterarrayoravariableoftypechar*charcolor[]=blue;char*colorPtr=blue;Rememberthatstringsrepresentedascharacterarraysendwith'\0'colorhas5elementsInputtingstringsUsescanfscanf(%s,word);Copiesinputintoword[]Donotneed&(becauseastringisapointer)Remembertoleaveroominthearrayfor'\0'Chapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C78.2FundamentalsofStringsandCharactersAnarrayofcharactersUsedtostoretextAnotherwaytoinitialize:charstr[]=Text;Insteadofcharstr[]={‘T’,’e’,’x’,’t’};Chapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C88.2FundamentalsofStringsandCharactersWheredoesitend?'s''#''''f''d''y''4''7''$''_''e''g''d''.''p''v'….….'H''e''l''l''o''''w''o''r''l''d''g''d''.''p''v'….….'H''e''l''l''o''''w''o''r''l''d''d''.''p''v'….….'\0'TerminatorstrChapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C98.2FundamentalsofStringsandCharactersStringsterminatewiththe'\0'character(ASCIIcode0)ThisisaconventionusedtoknowwherethestringendsItmeansthatinordertoholdastringofNcharactersweneedanarrayoflengthN+1So:charstr[]=Text;Isequalto:charstr[]={'T',‘e',‘x',‘t','\0'};Chapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C108.2FundamentalsofStringsandCharactersWhatdoesitprint?intmain(void){charstr[]=I'mafullstring;printf(%s\n,str);str[7]='o';str[8]='o';printf(%s\n,str);str[10]='\0';printf(%s\n,str);str[10]='s';printf(%s\n,str);return0;}I’mafullstringI’mafoolstringI’mafoolI’mafoolsstringChapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C118.2FundamentalsofStringsandCharactersReadingstringsThereareseveralwaysofacceptingstringsasinputfromtheuserOnewayistoreadcharacterbycharacterusinggetchar()Chapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C128.2FundamentalsofStringsandCharactersUsinggetchar()#defineMAX_LENGTH20intmain(void){charstr[MAX_LENGTH+1];/*Weneedonemoreplaceforthe'\0'*/charc;inti;printf(Pleaseenterastring:\n);i=0;c=getchar();while(c=0&&c!='\n'&&iMAX_LENGTH){str[i]=c;++i;c=getchar();}str[i]='\0';/*Terminatethestring*/printf(Thestringyouenteredis:%s\n,str);return0;}Chapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C138.2FundamentalsofStringsandCharactersReadingstringsAnotherwayistousescanfToreadinastringtoavariablestr,write:scanf(%s,str);Notethereisno‘&’sign!!!Chapter8CharactersandStringsFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C148.2FundamentalsofStringsandCharactersReadingstringsscanfreadsinlettersuntilaspaceornewline('\n')isencounteredThemaximumlengthcanbestatedintheparentheses:RememberthatstringsrepresentedToreadinastringtoavariablestr,write:scanf(%10s,str);Thiswillread10letters,plusthe'\0'sign(sostrshouldbeofsize11)Chapter8CharactersandStringsFacultyof

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

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

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

×
保存成功