ImageVerifierCode 换一换
格式:PPT , 页数:49 ,大小:1.38MB ,
资源ID:3357094      下载积分:20 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-3357094.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(高级语言程序设计 chapter9.ppt)为本站会员(dreamzhangning)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

高级语言程序设计 chapter9.ppt

1、A First Book of ANSI C Fourth Edition,Chapter 9 Character Strings,A First Book of ANSI C, Fourth Edition,2,Objectives,String Fundamentals Library Functions Input Data Validation Formatting Strings (Optional) Case Study: Character and Word Counting Common Programming and Compiler Errors,A First Book

2、of ANSI C, Fourth Edition,3,String Fundamentals,A string literal is any sequence of characters enclosed in double quotes “Good Morning!“ Also called: string constant, string value, string A string is stored as an array of characters terminated by an end-of-string symbolic constant named NULL (0),A F

3、irst Book of ANSI C, Fourth Edition,4,String Input and Output,gets() accepts and stores the characters typed at the terminal into the character array Pressing the Enter key generates a newline character, n, which is interpreted by gets() as the end-of-character entry All the characters encountered b

4、y gets(), except the newline character, are stored in the message array,A First Book of ANSI C, Fourth Edition,5,String Input and Output (continued),A First Book of ANSI C, Fourth Edition,6,String Input and Output (continued),Sample run: Enter a string: This is a test input of a string of characters

5、. The string just entered is: This is a test input of a string of characters.,A First Book of ANSI C, Fourth Edition,7,String Input and Output (continued),A printf() function call can be used in place of a puts() function call printf(“%sn“,message); puts(message); This correspondence between the out

6、put functions is not duplicated by the input functions scanf() and gets() scanf() reads a set of characters up to either a blank space or a newline character scanf(“%s“,message); /No & is required gets() stops accepting characters only when a newline is detected,A First Book of ANSI C, Fourth Editio

7、n,8,String Input and Output (continued),A First Book of ANSI C, Fourth Edition,9,String Processing,A First Book of ANSI C, Fourth Edition,10,String Processing (continued),A First Book of ANSI C, Fourth Edition,11,NOTE: Because the expression string2i is only 0 at the end of a string and non-0 for ev

8、ery other character, the expression while (string2i != 0) can be replaced by the simpler expression while (string2i).,String Processing (continued),A First Book of ANSI C, Fourth Edition,12,String Processing (continued),A First Book of ANSI C, Fourth Edition,13,Be careful: omitting the parentheses c

9、auses the entire expression to be equivalent to c = (getchar() != n),String Processing (continued),A First Book of ANSI C, Fourth Edition,14,String Processing (continued),A First Book of ANSI C, Fourth Edition,15,Library Functions,Note: Attempting to copy a larger string into a smaller string causes

10、 the copy to overflow the destination array beginning with the memory area immediately following the last array element.,A First Book of ANSI C, Fourth Edition,16,Library Functions (continued),A First Book of ANSI C, Fourth Edition,17,Library Functions (continued),When comparing strings, their indiv

11、idual characters are evaluated in pairs; if a difference is found, the string with the first lower character is the smaller one “Good Bye“ is less than “Hello“ because the first G in Good Bye is less than the first H in Hello “Hello“ is less than “Hello “ because the 0 terminating the first string i

12、s less than the in the second string “123“ is greater than “122“ because 3 in 123 is greater than 2 in 122 “1237“ is greater than “123“ because 7 in 1237 is greater than 0 in 123,A First Book of ANSI C, Fourth Edition,18,Library Functions (continued),A First Book of ANSI C, Fourth Edition,19,Library

13、 Functions (continued),A First Book of ANSI C, Fourth Edition,20,Library Functions (continued),Sample output: Hello is less than Hello thereThe length of string1 is 5 characters The length of string2 is 11 charactersAfter concatenation, string1 contains the string value Hello there World! The length

14、 of this string is 18 charactersType in a sequence of characters for string2: Its a wonderful day After copying string2 to string1, the string value in string1 is: Its a wonderful day The length of this string is 20 charactersThe starting address of the string1 string is: 1244836,A First Book of ANS

15、I C, Fourth Edition,21,Character Routines,A First Book of ANSI C, Fourth Edition,22,Character Routines (continued),A First Book of ANSI C, Fourth Edition,23,Conversion Routines,A First Book of ANSI C, Fourth Edition,24,Conversion Routines (continued),A First Book of ANSI C, Fourth Edition,25,Input D

16、ata Validation,Successful programs always try to anticipate invalid data and isolate such data from being accepted and processed First validate that the data is of the correct type; if not, request the user to re-enter the data Explain why the entered data was invalid One of the most common methods

17、of validating input data is to accept all numbers as strings Each character can then be checked to ensure that it complies with the data type being requested,A First Book of ANSI C, Fourth Edition,26,Input Data Validation (continued),A First Book of ANSI C, Fourth Edition,27,Input Data Validation (c

18、ontinued),A First Book of ANSI C, Fourth Edition,28,Input Data Validation (continued),We can use isvalidInt() in a loop that continually requests an integer until a valid integer value is entered Set an integer variable named isanInt to 0 doAccept a string valueIf the string value does not correspon

19、d to an integerDisplay the error message “Invalid integer - Please re-enter: “Send control back to expression being tested by the do-while statementSet isanInt to 1 (this causes the loop to terminate) while(isanInt is 0) Return the integer corresponding to the entered string,A First Book of ANSI C,

20、Fourth Edition,29,Input Data Validation (continued),A First Book of ANSI C, Fourth Edition,30,Creating a Personal Library,Programmers create their own libraries of functions This permits the functions to be incorporated in any program without further expenditure of coding time Each file in a library

21、 contains related functions #include #include “C:mylibrarydataChecks.h“ The #include statement for dataChecks.h must be placed after the #include statements for the stdio.h and stdlib.h header files (the functions in dataChecks.h require stdio.h and stdlib.h functions to correctly compile),A First B

22、ook of ANSI C, Fourth Edition,31,Formatting Strings,Examples: printf(“|%25s|“,“Have a Happy Day“); | Have a Happy Day| printf(“|%-25s|“,“Have a Happy Day“); |Have a Happy Day | printf(“|%25.12s|“,“Have a Happy Day“); | Have a Happy| printf(“|%.12s|“,“Have a Happy Day“); |Have a Happy|,A First Book o

23、f ANSI C, Fourth Edition,32,In-Memory String Conversions,The sprintf() and sscanf() functions provide capabilities for writing and scanning strings to and from memory variables sprintf(disStrn,“%d %d“, num1, num2); sscanf(data,“%c%lf %d“, “07/01/94“,A First Book of ANSI C, Fourth Edition,33,Format S

24、trings,The control string containing the conversion control sequences need not be explicitly contained within the function printf(“$%5.2f %d“,num1,num2); Or, char fmat = “$%5.2f %d“; printf(fmat,num1,num2); Useful for listing format strings with other variable declarations at the beginning of a func

25、tion If you need to change a format, it is easy to find the desired control string without searching to locate the appropriate printf() or scanf() function calls,A First Book of ANSI C, Fourth Edition,34,Case Study: Character and Word Counting,We construct two string-processing functions Count the n

26、umber of characters in a string Count words in a sting What constitutes a word?,A First Book of ANSI C, Fourth Edition,35,Program Requirement: Character Counting,Pass a string to a function and have the function return the number of characters in the string Any character in the string (blank, printa

27、ble, or nonprintable character) is to be counted The end-of-string NULL character is not to be included in the final count,A First Book of ANSI C, Fourth Edition,36,Analyze the Problem,Determine the input data Determine the required outputs List the algorithm(s) relating the inputs to the outputs,A

28、First Book of ANSI C, Fourth Edition,37,Analyze the Problem (continued),A First Book of ANSI C, Fourth Edition,38,Code the Function,int countchar(char list) int i, count = 0;for(i = 0; listi != 0; i+)count+;return(count); ,A First Book of ANSI C, Fourth Edition,39,Test and Debug the Function,A First

29、 Book of ANSI C, Fourth Edition,40,Requirement Specification: Word Counting,The last word does not have a trailing blank More than one blank may be used between words Leading blanks may be used before the first word,A First Book of ANSI C, Fourth Edition,41,Analyze the Problem,Determine the input da

30、ta Determine the required outputs Algorithm: Set an integer variable named inaword to the symbolic constant NO Set the word count to 0 For all the characters in the arrayIf the current character is a blankset inaword to NOElse if (inaword equals NO)set inaword to the symbolic constant YESincrement t

31、he word countEndIf EndFor Return the count,A First Book of ANSI C, Fourth Edition,42,Code the Function,int countword(char list) #define YES 1 #define NO 0 int i, inaword, count = 0;inaword = NO;for(i = 0; listi != 0; i+)if (listi = )inaword = NO;else if (inaword = NO)inaword = YES;count+;return(coun

32、t); ,A First Book of ANSI C, Fourth Edition,43,Test and Debug the Function,A First Book of ANSI C, Fourth Edition,44,Test and Debug the Function (continued),A sample run using Program 9.11 follows: Type in any number of words: This is a test line with a bunch of words The number of words just entere

33、d is 10 Further tests that should be performed are Enter words with multiple spaces between them Enter words with leading spaces before the first word Enter words with trailing spaces after the last word Enter a sentence that ends in a period or question mark,A First Book of ANSI C, Fourth Edition,4

34、5,Common Programming Errors,Forgetting the terminating NULL character, 0, when processing existing strings in a character-by-character manner Forgetting to terminate a newly created character string with the NULL character Forgetting that the newline character, n, is a valid data input character For

35、getting to include the string.h, ctype.h, and stdlib.h header files when using the string library, character library, and conversion library functions, respectively,A First Book of ANSI C, Fourth Edition,46,Common Compiler Errors,A First Book of ANSI C, Fourth Edition,47,Common Compiler Errors (cont

36、inued),A First Book of ANSI C, Fourth Edition,48,Summary,A string is an array of characters terminated by the NULL (0) character Character arrays can be initialized using a string assignment of the form char arrayName = “text“; Strings can always be processed using standard array-processing techniqu

37、es The gets(), scanf(), and getchar() library functions can be used to input a string The puts(), printf(), and putchar() functions can be used to display strings,A First Book of ANSI C, Fourth Edition,49,Summary (continued),Many standard library functions exist for processing strings as a complete

38、unit The standard C library also includes individual character-handling functions (ctype.h) One of the major uses of strings is validating user input, which is an essential part of any program The conversion routines atoi() and atof() are provided in the stdlib.h header file for converting strings to integer and double-precision numeric values,

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报