《高级语言程序设计 chapter9.ppt》由会员分享,可在线阅读,更多相关《高级语言程序设计 chapter9.ppt(49页珍藏版)》请在金锄头文库上搜索。
1、AFirstBookofANSICFourthEdition Chapter9CharacterStrings AFirstBookofANSIC FourthEdition 2 Objectives StringFundamentalsLibraryFunctionsInputDataValidationFormattingStrings Optional CaseStudy CharacterandWordCountingCommonProgrammingandCompilerErrors AFirstBookofANSIC FourthEdition 3 StringFundamenta
2、ls Astringliteralisanysequenceofcharactersenclosedindoublequotes GoodMorning Alsocalled stringconstant stringvalue stringAstringisstoredasanarrayofcharactersterminatedbyanend of stringsymbolicconstantnamedNULL 0 AFirstBookofANSIC FourthEdition 4 StringInputandOutput gets acceptsandstoresthecharacter
3、stypedattheterminalintothecharacterarrayPressingtheEnterkeygeneratesanewlinecharacter n whichisinterpretedbygets astheend of characterentryAllthecharactersencounteredbygets exceptthenewlinecharacter arestoredinthemessagearray AFirstBookofANSIC FourthEdition 5 StringInputandOutput continued AFirstBoo
4、kofANSIC FourthEdition 6 StringInputandOutput continued Samplerun Enterastring Thisisatestinputofastringofcharacters Thestringjustenteredis Thisisatestinputofastringofcharacters AFirstBookofANSIC FourthEdition 7 StringInputandOutput continued Aprintf functioncallcanbeusedinplaceofaputs functioncallp
5、rintf s n message puts message Thiscorrespondencebetweentheoutputfunctionsisnotduplicatedbytheinputfunctionsscanf andgets scanf readsasetofcharactersuptoeitherablankspaceoranewlinecharacterscanf s message No isrequiredgets stopsacceptingcharactersonlywhenanewlineisdetected AFirstBookofANSIC FourthEd
6、ition 8 StringInputandOutput continued AFirstBookofANSIC FourthEdition 9 StringProcessing AFirstBookofANSIC FourthEdition 10 StringProcessing continued AFirstBookofANSIC FourthEdition 11 NOTE Becausetheexpressionstring2 i isonly0attheendofastringandnon 0foreveryothercharacter theexpressionwhile stri
7、ng2 i 0 canbereplacedbythesimplerexpressionwhile string2 i StringProcessing continued AFirstBookofANSIC FourthEdition 12 StringProcessing continued AFirstBookofANSIC FourthEdition 13 Becareful omittingtheparenthesescausestheentireexpressiontobeequivalenttoc getchar n StringProcessing continued AFirs
8、tBookofANSIC FourthEdition 14 StringProcessing continued AFirstBookofANSIC FourthEdition 15 LibraryFunctions Note Attemptingtocopyalargerstringintoasmallerstringcausesthecopytooverflowthedestinationarraybeginningwiththememoryareaimmediatelyfollowingthelastarrayelement AFirstBookofANSIC FourthEdition
9、 16 LibraryFunctions continued AFirstBookofANSIC FourthEdition 17 LibraryFunctions continued Whencomparingstrings theirindividualcharactersareevaluatedinpairs ifadifferenceisfound thestringwiththefirstlowercharacteristhesmallerone GoodBye islessthan Hello becausethefirst G inGoodByeislessthanthefirs
10、t H inHello Hello islessthan Hello becausethe 0 terminatingthefirststringislessthanthe inthesecondstring 123 isgreaterthan 122 because 3 in123isgreaterthan 2 in122 1237 isgreaterthan 123 because 7 in1237isgreaterthan 0 in123 AFirstBookofANSIC FourthEdition 18 LibraryFunctions continued AFirstBookofA
11、NSIC FourthEdition 19 LibraryFunctions continued AFirstBookofANSIC FourthEdition 20 LibraryFunctions continued Sampleoutput HelloislessthanHellothereThelengthofstring1is5charactersThelengthofstring2is11charactersAfterconcatenation string1containsthestringvalueHellothereWorld Thelengthofthisstringis1
12、8charactersTypeinasequenceofcharactersforstring2 It sawonderfuldayAftercopyingstring2tostring1 thestringvalueinstring1is It sawonderfuldayThelengthofthisstringis20charactersThestartingaddressofthestring1stringis 1244836 AFirstBookofANSIC FourthEdition 21 CharacterRoutines AFirstBookofANSIC FourthEdi
13、tion 22 CharacterRoutines continued AFirstBookofANSIC FourthEdition 23 ConversionRoutines AFirstBookofANSIC FourthEdition 24 ConversionRoutines continued AFirstBookofANSIC FourthEdition 25 InputDataValidation Successfulprogramsalwaystrytoanticipateinvaliddataandisolatesuchdatafrombeingacceptedandpro
14、cessedFirstvalidatethatthedataisofthecorrecttype ifnot requesttheusertore enterthedataExplainwhytheentereddatawasinvalidOneofthemostcommonmethodsofvalidatinginputdataistoacceptallnumbersasstringsEachcharactercanthenbecheckedtoensurethatitcomplieswiththedatatypebeingrequested AFirstBookofANSIC Fourth
15、Edition 26 InputDataValidation continued AFirstBookofANSIC FourthEdition 27 InputDataValidation continued AFirstBookofANSIC FourthEdition 28 InputDataValidation continued WecanuseisvalidInt inaloopthatcontinuallyrequestsanintegeruntilavalidintegervalueisenteredSetanintegervariablenamedisanIntto0doAc
16、ceptastringvalueIfthestringvaluedoesnotcorrespondtoanintegerDisplaytheerrormessage Invalidinteger Pleasere enter Sendcontrolbacktoexpressionbeingtestedbythedo whilestatementSetisanIntto1 thiscausesthelooptoterminate while isanIntis0 Returntheintegercorrespondingtotheenteredstring AFirstBookofANSIC FourthEdition 29 InputDataValidation continued AFirstBookofANSIC FourthEdition 30 CreatingaPersonalLibrary ProgrammerscreatetheirownlibrariesoffunctionsThispermitsthefunctionstobeincorporatedinanyprogr