Strings CSCI 112 Programming in C String basics

Strings CSCI 112: Programming in C

String basics • C stores strings as arrays of char, terminated by the NULL character • The NULL character is used so that C knows where the end of the string is when it iterates over the array. • It is important to always include it when you manually build string arrays, but C will automatically append it when you use most built-in string builders. 2 2

Declaration & Instantiation Declare a string as a simple array of characters This allocates space for a String up to 29 characters plus the NULL character. 3 3

Declaration & Instantiation Declare a string as a simple array of characters This allocates space for a String up to 29 characters plus the NULL character. 4 4

Declaration & Instantiation can be done with a string literal: This string can hold up to 19 characters plus null character, but not all space is used: 5 5

Arrays of String An array of Strings is a 2 D array—that is, an array of char arrays: 6 6

Arrays of String An array of Strings is a 2 D array—that is, an array of char arrays: As with 2 D arrays, the lengths of all but first dimension are required. Compiler needs to know how much space to allocate for each string, but can infer from literal how many strings there are. 7 7

String I/O The %s placeholder can be used in printf and scanf to denote the char array type: where string_var is a char array (or pointer to first element of char array) 8 8

String I/O The %s placeholder can be used in printf and scanf to denote the string type: where string_var is a char array (or pointer to first element of char array) C will print out the elements in string_var up to the null character—this is why we need that at the end of strings! 9 9

String I/O The %s placeholder can be used in printf and scanf to denote the string type: where string_var is a char array (or pointer to first element of char array) C will print out the elements in string_var up to the null character—this is why we need that at the end of strings! We can specify a minimum width, such as %#s (If string is less than # chars long, padding is added to left of string. Negative value of # will add padding to right of string. ) 10 10

String I/O %s is used in scanf to take in string input from the console: scanf("%s", char_array); 11 11

String I/O %s is used in scanf to take in string input from the console: scanf("%s", char_array); Why is there no & before char_array? 12 12

String I/O %s is used in scanf to take in string input from the console: scanf("%s", char_array); Why is there no & before char_array? Warning: a big pitfall of dealing with strings in C is overflow: what if the user enters a string that’s larger than char_array? We have to be careful of this, including when we start using some the C Library string functions 13 13

String I/O The scanf function will place characters of a string into the char array until it sees whitespace (or newline), at which point it adds the null character into the array and stops. 14 14

Example: Parallel arrays of strings 15 15

C Library Functions and strings Because strings are really arrays, a lot of the string manipulation we’re used to doing in other languages would be much harder in C if it weren’t for some built in library functions which handle the array manipulation for use. To use these functions, the string. h header file should be included. Page 463 of book gives a great table about some common string. h functions. 16 16

C Library: Assignment We can assign a string literal to a char array when we declare it, but we cannot reassign a value to it. (Since it’s an array, and the name of an array can’t go on the left-hand side of an assignment statement after declaration. ) char name[20] = "Fred"; name = "Greg"; 17 17

C Library: Assignment The strncpy function copies a given number of characters from one char array (string) into another. The book talks about the strcpy (no n) function—be careful! It can cause an overflow, overwriting your other variables or throwing a runtime error. It does not limit the number of characters it will try to copy from one to the other. 18 18

C Library: Assignment The strncpy function copies a given number of characters from one char array (string) into another. Destination string (must be a char array) Source string (does not have to be a literal, could be a char array) Maximum number of characters to copy) If the source string is shorter than the max number of characters to copy, the remaining spaces (up to max) will be filled with null characters. 19 19

C Library: Assignment The strncpy function copies a given number of characters from one char array (string) into another. Destination string (must be a char array) Source string (does not have to be a literal, could be a char array) Maximum number of characters to copy) strncpy does NOT add the null terminator automatically, unless there’s remaining space. We need to do this manually. 20 20

C Library: Assignment 21 21

C Library: Substrings The strncpy function can be sued to extract parts (substrings) of a string. Let’s say we want to get just the day from the string “January 30, 2005” How can we do this with strncpy, given the following setup? char date[17] = "January 30, 2005"; char day[3]; HINT: Remember that the first two arguments of strncpy take char pointers—they don’t have to be (though often are) the name of an array! 22 22

C Library: Substrings The strncpy function can be sued to extract parts (substrings) of a string. Let’s say we want to get just the day from the string “January 30, 2005” char date[17] = "January 30, 2005"; char day[3]; strncpy(day, &date[8], 2); day[2] = ‘ ’; Notice that we have to manually add in the null character! 23 23

C Library: Substrings The strncpy function can be sued to extract parts (substrings) of a string. Let’s say we want to get just the day from the string “January 30, 2005” char date[16] = "January 30, 2005"; char day[3]; strncpy(day, &date[8], 2); day[2] = ‘ ’; The first argument points to the first character in date, and the second to the 8 th character. (I. e. , the “starting point” for the copy is the 8 th character of day) 24 24

Example: Break string into components We want to take a string such as Hey. There. Im. AString and print out its components separated by capital letter. 25 25