1 / 62

Chapter 8 Characters and Strings

Chapter 8 Characters and Strings. Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. OBJECTIVES. Fundamentals of strings and characters Character-handling library String-conversion functions

Télécharger la présentation

Chapter 8 Characters and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.

  2. OBJECTIVES • Fundamentals of strings and characters • Character-handling library • String-conversion functions • Standard input/output library functions • String-manipulation functions of the string-handling library • Comparison functions of the string-handling library • Search functions of the string-handling library • Other functions of the string-handling library • Review

  3. Introduction • Introduce some standard library functions • Easy string and character processing • Programs can process characters, strings, lines of text, and blocks of memory • These techniques used to make • Word processors • Page layout software • Typesetting programs

  4. Fundamentals of Strings and Characters • Characters • Building blocks of programs • Every program is a sequence of meaningfully grouped characters • Character constant • An int value represented as a character in single quotes • 'z' represents the integer value of z • Strings • Series of characters treated as a single unit • Can include letters, digits and special characters (*, /, $) • String literal (string constant) - written in double quotes • "Hello" • Strings are arrays of characters • String a pointer to first character • Value of string is the address of first character

  5. Fundamentals of Strings and Characters • String definitions • Define as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; • Remember that strings represented as character arrays end with '\0' • color has 5 elements • Inputting strings • Use scanf scanf("%s", word); • Copies input into word[] • Do not need & (because a string is a pointer) • Remember to leave room in the array for '\0'

  6. OBJECTIVES • Fundamentals of strings and characters • Character-handling library • String-conversion functions • Standard input/output library functions • String-manipulation functions of the string-handling library • Comparison functions of the string-handling library • Search functions of the string-handling library • Other functions of the string-handling library • Review

  7. Character Handling Library • Character handling library • Includes functions to perform useful tests and manipulations of character data • Each function receives a character (an int) or EOF as an argument • The following slide contains a table of all the functions in <ctype.h>

  8. Character-handling library functions

  9. isdigittests if a character is a decimal digit isalphatests if a character is a letter • fig08_02.c (1 of 3 )

  10. isdigittests if a character is a decimal digit or a letter isxdigittests if a character is a hexadecimal digit • fig08_02.c (2 of 3 )

  11. fig08_02.c (3 of 3 )

  12. fig08_03.c (1 of 2 ) islowertests if a character is a lowercase letter isuppertests if a character is an uppercase letter

  13. toupperand tolowerconvert letters to upper or lower case • fig08_03.c (2 of 2 )

  14. fig08_04.c (1 of 3 ) isspacetests if a character is a whitespace character iscntrltests if a character is a control character

  15. ispuncttests if a character is a punctuation character isprinttests if a character is a printing character • fig08_04.c (2 of 3 )

  16. isgraphtests if a character is a printing character that is not a space • fig08_04.c (3 of 3 )

  17. OBJECTIVES • Fundamentals of strings and characters • Character-handling library • String-conversion functions • Standard input/output library functions • String-manipulation functions of the string-handling library • Comparison functions of the string-handling library • Search functions of the string-handling library • Other functions of the string-handling library • Review

  18. String Conversion Functions • Conversion functions • In <stdlib.h> (general utilities library) • Convert strings of digits to integer and floating-point values

  19. fig08_06.c atofconverts a string to a double Note: If the converted value cannot be represented, e.g., if the first character of the string is a letter, the behavior of function atof is undefined.

  20. atoiconverts a string to an int • fig08_07.c

  21. atolconverts a string to a long • fig08_08.c

  22. strtodconverts a piece of a string to a double • fig08_09.c

  23. fig08_10.c strtolconverts a piece of a string to a long Note: 1. Using NULL for the second argument causes the remainder of the string to be ignored. 2. The third argument, 0, indicates that the value to be converted can be in octal (base 8), decimal (base 10) or hexadecimal (base 16) format.

  24. fig08_11.c strtoulconverts a piece of a string to an unsignedlong

  25. OBJECTIVES • Fundamentals of strings and characters • Character-handling library • String-conversion functions • Standard input/output library functions • String-manipulation functions of the string-handling library • Comparison functions of the string-handling library • Search functions of the string-handling library • Other functions of the string-handling library • Review

  26. Standard Input/Output Library Functions • Functions in <stdio.h> • Used to manipulate character and string data

  27. Recursive Functions • The function calls itself in its body. • Do itself again and again until reach the base case. • Analyze actions by diagrams.

  28. getsreads a line of text from the user • fig08_13.c (1 of 2 )

  29. putcharprints a single character on the screen • fig08_13.c (2 of 2 )

  30. Detailed Analysis • Step 1: reverse(“abc”) • Step 2: reverse(&sPtr[1])  reverse(“bc”) (Do first) • putchar(sPtr[0])  putchar(‘a’) (Do next) • Step 2: reverse(“bc”) • Step 3: reverse(&sPtr[1])  reverse(“c”) (Do first) • putchar(sPtr[0])  putchar(‘b’) (Do next) • Step 3: reverse(“c”) • Step 4: reverse(&sPtr[1])  reverse(“\0”) (Do first) • putchar(sPtr[0])  putchar(‘c’) (Do next) • Step 4: reverse(“\0”) • base case  return • Step 4  Step 3  Step 2  Step 1 “cba”

  31. putsprints a line of text on the screen getcharreads a single character from the user • fig08_14.c (1 of 2 )

  32. fig08_14.c (2 of 2 )

  33. fig08_15.c sprintfprints a line of text into an array like printf prints text on the screen

  34. fig08_16.c sscanfreads a line of text from an array like scanf reads text from the user

  35. OBJECTIVES • Fundamentals of strings and characters • Character-handling library • String-conversion functions • Standard input/output library functions • String-manipulation functions of the string-handling library • Comparison functions of the string-handling library • Search functions of the string-handling library • Other functions of the string-handling library • Review

  36. String Manipulation Functions of the String Handling Library • String handling library has functions to • Manipulate string data • Search strings • Tokenize strings • Determine string length

  37. fig08_18.c strcpycopies string x into character array y strncpycopies 14 characters of string x into character array z Note that strncpydoes not automatically append a null character

  38. fig08_19.c (1 of 2 ) strcatadds the characters of string s2 to the end of string s1 strncatadds the first 6 characters of string s1 to the end of string s3. A terminating null character is automatically appended to the result.

  39. fig08_19.c (2 of 2 )

  40. OBJECTIVES • Fundamentals of strings and characters • Character-handling library • String-conversion functions • Standard input/output library functions • String-manipulation functions of the string-handling library • Comparison functions of the string-handling library • Search functions of the string-handling library • Other functions of the string-handling library • Review

  41. Comparison Functions of the String Handling Library • Comparing strings • Computer compares numeric ASCII codes of characters in string • Appendix D has a list of character codes int strcmp( const char *s1, const char *s2 ); • Compares string s1 to s2 • Returns a negative number if s1 < s2, zero if s1 == s2 or a positive number if s1 > s2 int strncmp( const char *s1, const char *s2,size_t n ); • Compares up to n characters of string s1 to s2 • Returns values as above

  42. strcmpcompares string s1 to string s2 • fig08_21.c (1 of 2 )

  43. strncmpcompares the first 6 characters of string s1 to the first 6 characters of string s3 • fig08_21.c (2 of 2 )

  44. OBJECTIVES • Fundamentals of strings and characters • Character-handling library • String-conversion functions • Standard input/output library functions • String-manipulation functions of the string-handling library • Comparison functions of the string-handling library • Search functions of the string-handling library • Other functions of the string-handling library • Review

  45. Search Functions of the String-Handling Library

  46. Search Functions of the String-Handling Library

  47. strchrsearches for the first instance of character1 in string • fig08_23.c (1 of 2 )

  48. fig08_23.c (2 of 2 )

  49. fig08_24.c strcspnreturns the length of the initial segment of string1 that does not contain any characters in string2

  50. fig08_27.c strspnreturns the length of the initial segment of string1 that contains only characters from string2

More Related