Chapter08

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Chapter 8 - Characters and Strings : 

Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions 8.5 Standard Input/Output Library Functions 8.6 String Manipulation Functions of the String Handling Library 8.7 Comparison Functions of the String Handling Library 8.8 Search Functions of the String Handling Library 8.9 Memory Functions of the String Handling Library 8.10 Other Functions of the String Handling Library

8.1 Introduction : 

8.1 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

8.2 Fundamentals of Strings and Characters : 

8.2 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

8.2 Fundamentals of Strings and Characters (II) : 

8.2 Fundamentals of Strings and Characters (II) Strings Series of characters treated as a single unit Can include letters, digits, and certain 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

8.2 Fundamentals of Strings and Characters (III) : 

8.2 Fundamentals of Strings and Characters (III) String declarations Declare 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

8.2 Fundamentals of Strings and Characters (IV) : 

8.2 Fundamentals of Strings and Characters (IV) Inputting strings Use scanf scanf("%s", word); Copies input into word[], which does not need & (because a string is a pointer) Remember to leave space for '\0'

8.3 Character Handling Library : 

8.3 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

8.3 Character Handling Library (II) : 

8.3 Character Handling Library (II) In <ctype.h>

Slide 9: 

1. Load header 2. Perform tests 3. Print

Slide 10: 

Program Output According to isdigit: 8 is a digit # is not a digit According to isalpha: A is a letter b is a letter & is not a letter 4 is not a letter According to isalnum: A is a digit or a letter 8 is a digit or a letter # is not a digit or a letter According to isxdigit: F is a hexadecimal digit J is not a hexadecimal digit 7 is a hexadecimal digit $ is not a hexadecimal digit f is a hexadecimal digit

8.4 String Conversion Functions : 

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

Slide 12: 

1. Initialize variable 2. Convert string 2.1 Assign to variable 3. Print Program Output The string "99.0" converted to double is 99.000 The converted value divided by 2 is 49.500

8.5 Standard Input/Output Library Functions : 

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

Slide 14: 

1. Initialize variables 2. Input 3. Print 3.1 Function definition (note recursion) Enter a line of text: Characters and Strings The line printed backwards is: sgnirtS dna sretcarahC

8.6 String Manipulation Functions of the String Handling Library : 

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

Slide 16: 

1. Initialize variables 2. Function calls 3. Print Program Output s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year

8.7 Comparison Functions of the String Handling Library : 

8.7 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 (s1 < s2), zero (s1 == s2), or a positive number (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

8.8 Search Functions of the String Handling Library : 

8.8 Search Functions of the String Handling Library

Slide 19: 

1. Initialize variables 2. Function calls 3. Print Program Output string1 = The value is 3.14159 string2 = aehi lsTuv The length of the initial segment of string1 containing only characters from string2 = 13

Slide 20: 

1. Initialize variables 2. Function calls 3. Print Program Output The string to be tokenized is: This is a sentence with 7 tokens The tokens are: This is a sentence with 7 tokens

8.9 Memory Functions of the String- handling Library : 

8.9 Memory Functions of the String- handling Library Memory Functions In <stdlib.h> Manipulate, compare, and search blocks of memory Can manipulate any block of data Pointer parameters are void * Any pointer can be assigned to void *, and vice versa void * cannot be dereferenced Each function receives a size argument specifying the number of bytes (characters) to process

8.9 Memory Functions of the String- handling Library (II) : 

8.9 Memory Functions of the String- handling Library (II) "Object" refers to a block of data

Slide 23: 

1. Initialize variables 2. Function calls 3. Print Program Output The string in array x before memmove is: Home Sweet Home The string in array x after memmove is: Sweet Home Home

8.10 Other Functions of the String Handling Library : 

8.10 Other Functions of the String Handling Library char *strerror( int errornum ); Creates a system-dependent error message based on errornum Returns a pointer to the string size_t strlen( const char *s ); Returns the number of characters (before NULL) in string s

Slide 25: 

1. Function call 2. Print Program Output No such file or directory

authorStream Live Help