Overview CSE 142 Computer Programming I Concepts this lecture String constants Null terminated array representation String library string h String initializers Arrays of strings Strings T 1 2000 UW CSE Chapter 9 T 2 Character Data in Programs Names messages labels headings etc Read Sections 9 1 9 2 and 9 4 All of these are common in computer applications 9 1 String Basics Table 9 1 for summary of common functions All involve characters usually multiple characters 9 2 String Assignment 9 3 String Concatenation 9 4 String Comparison T 3 So far our ability to handle these things in C is very limited T 4 Characters and Strings String Representation Character constants literals single quotes Strings are stored in char arrays Programming convention a null character 0 is stored at the end a A 0 1 n B i l 0 null character string representation String constants literals double quotes sample Bill is very rich The answer is 2f n T 5 s amp l e 0 T 6 N 0 in Strings Leaving Room for 0 Character arrays holding strings must have room for 0 following the actual data The empty string occupies 1 char Character and string constants are not the same x and x are different How 0 is not included in strings automatically 0 is included in string constants automatically Programmer must take pains to be sure 0 is present elsewhere when needed s amp l e s amp l e 0T 7 String Operations What You Can t Do Common needed operations Copy assignment Compare Find length Concatenate combine strings I O Strings are arrays Unfortunately Can t compare strings with 0 T 8 They have the limitations of arrays Can t assign one string to another with s amp l e T 9 0 String Library string h But there are library functions to help do such things s a m p l e 0 T 10 String Length strlen strlen returns the length of its string argument Does not count the null 0 at the end Standard C includes a library of string functions use include string h Library functions Require proper null terminated 0 strings as arguments Produce null terminated strings as results usually Examples The length of A is 1 The length of is 0 k strlen null terminated string s amp l e T 11 0 stores 22 in k T 12 N A strlen implementation String Assignment strcpy return the length of string s i e number of characters before terminating 0 or equivalently index of first 0 int strlen char s int n 0 while s n 0 n n 1 return n T 13 strcpy dest source Copies characters from source to dest Copies up to and including the first 0 found Be sure that dest is large enough to hold the result T 14 String Assignment Examples String Assignment Examples include string h char medium 21 char medium 21 char big 1000 char small 5 char small 5 strcpy big medium strcpy big Bob strcpy medium Four score and seven big Four score and seven 0 char big 1000 medium Four score and seven 0 T 15 String Assignment Dangers char big 1000 char small 5 strcpy small big looks like trouble small Bob 0 small Four score and seven 0 T 16 A strcpy implementation char medium 21 strcpy small medium big Bob 0 score and seven 0 T 17 copy source string into dest stopping with 0 void strcpy char dest char source int i 0 while source i 0 dest i source i i dest i 0 T 18 N Appending and Concatenation String Concatenation strcat To append means to place one string directly after another string h function strcat dest source chop appended to lamb should result in lambchop Also referred to as concatenation Appends characters from source to dest Copy is stored starting at first 0 in dest Copies up to and including the first 0 in source Be sure that dest is large enough T 19 Using strcat 1 T 20 Using strcat 2 include string h char str1 5 str2 5 str3 11 strcpy str3 str1 strcat str3 str2 strcpy str1 lamb strcpy str2 chop str1 l a m b 0 str2 c h o p 0 str3 T 21 str1 l a m b 0 str2 c h o p 0 0 str3 l a m b c h o p 0 T 22 Comparing Strings String Comparison strcmp strcmp compares corresponding characters until it finds a mismatch strcmp s1 s2 Compares s1 to s2 and returns an int describing the comparison lamb is less than wolf lamb is less than lamp lamb is less than lambchop Negative if s1 is less than s2 Zero if s1 equals s2 Positive if s1 is greater than s2 T 23 T 24 N Using strcmp 1 Using strcmp 2 Don t treat the result of strcmp as a Boolean If you treat the result of strcmp as a Boolean it probably won t do what you want Test the result as an integer if strcmp s1 s2 printf yikes if strcmp s1 s2 0 printf same n prints yikes if s1 and s2 are different T 25 String I O T 26 Spot the Security Hole scanf and printf can read and write C strings Format code is s printf assumes 0 is present scanf will automatically insert 0 at the end Be sure the array has room for it define MAX INPUT 200 char buffer MAX INPUT scanf s buffer T 27 Many Functions in string h strcat strncat concatenation strcmp strncmp comparison strtod strtol strtoul conversion Using Libraries of Functions To use strings effectively in C use functions from string h Using libraries is very typical of C programming ANSI C standard libraries such as stdio h string h ctype h math h Lots of others check your favorite reference Related useful functions in ctype h operations on a single char convert case to upper or lower check category is char a number etc many others T 28 Application specific libraries thousands of them exist T 29 You can t be an effective programmer without being able to quickly master new libraries of functions T 30 N Bonus String Initializers Bonus Arrays of Strings char pet 5 l a m b 0 char month 12 10 char pet 5 pet 0 l pet 1 a pet 2 m pet 3 b pet 4 0 January February all equivalent September char pet 5 lamb longest month 9 letters But not char pet 5 pet lamb No array assignment in C Remember that initializers are not assignment statements December T 31 printf s is hot n month 7 August T 32 Strings Summary Definition Null terminated array of char Strings are not fully a type of C They share most limitations of arrays scanf printf s string h library functions Assignment strcpy Length strlen strcat and many others T 33 Major Pitfall overrunning available space N
View Full Document
Unlocking...