Chapter 9 CSCE150A CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Introduction 9.1 String Basics Basics Basics 9.2 String Library Functions: Assignment and Substrings String Library String Library Lecture 07 - Strings 9.3 Longer Strings: Concatenation and Whole-Line Input Substrings Substrings Line Scanning Line Scanning 9.4 String Comparison Sorting Sorting 9.6 Character Operations Stephen Scott Command Command Line Line 9.7 String-to-Number and Number-to-String Conversion (Adapted from Christopher M. Bourke) Arguments Arguments 9.8 Common Programming Errors Misc Misc Fall 2009 1 / 51 2 / 51 Strings Strings CSCE150A CSCE150A Introduction Introduction Basics Basics A collection of characters is called a string Until now we have only dealt with single characters String Library String Library C has no string data type Substrings char myChar = ’A’ , ’\n’ Substrings Instead, strings are arrays of characters, char myString[] , Line Scanning Line Scanning Processing and manipulating single characters is too limiting char myName[20] Sorting Sorting Need a way for dealing with groups of characters Necessary to represent textual data, communicate with users in a Command Command Line Line readable manner Arguments Arguments Misc Misc 3 / 51 4 / 51 String Basics Static Strings CSCE150A CSCE150A Introduction Introduction Calls to scanf or printf used a string constant as the first Basics Basics argument. Static strings cannot be changed during the execution of the program String Library String Library We have also dealt with static strings : "Hello World!" Substrings Substrings They cannot be manipulated or processed printf("a = %d\n", a) Line Scanning Line Scanning May only be changed by recompiling printf("Average = %.2f", avg) Sorting Sorting Stored in an array of a fixed size Each string above is a string of 12, 7, and 14 characters, respectively Command Command Line Line Arguments Arguments It’s possible to use a preprocessor directive: Misc Misc #define INSUFF_DATA "Insufficient Data" 5 / 51 6 / 51
Declaring and Initializing String Variables Declaring and Initializing String Variables CSCE150A CSCE150A Strings are character arrays Declaration is the same, just use char Introduction Introduction char string_var[100]; You can declare and initialize in one line Basics Basics char myName[30]; Be sure to use the double quotes String Library String Library myName will hold strings anywhere from 0 to 29 characters long char myName[30] = "Brian"; Substrings Substrings Individual characters can be accessed/set using indices Line Scanning Line Scanning You need not specify the size of the array when declaring-initializing Sorting Sorting in one line: 1 myName [0] = ’B’; Command Command Line Line 2 myName [1] = ’r’; char myName[] = "Brian"; Arguments Arguments 3 myName [2] = ’i’; C will create a character array large enough to hold the string Misc Misc 4 myName [3] = ’a’; 5 myName [4] = ’n’; 6 printf("First initial: %c.\n", myName [0]); 7 / 51 8 / 51 Null Terminating Character Null Terminating Character Example CSCE150A CSCE150A Introduction Introduction char str[20] = "Initial value"; will produce the following in Basics C needs a way to tell where the end of a string is Basics memory: String Library String Library With arrays, it is your responsibility to ensure you do not access Substrings Substrings memory outside the array [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Line Scanning Line Scanning To determine where the string ends, C uses the null-terminating I n i t i a l v a Sorting Sorting character : ’\0’ [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] Command Command Line Line ? ? ? ? ? ? l u e \0 Character with ASCII code 0 Arguments Arguments Misc Misc 9 / 51 10 / 51 Arrays of Strings Printing Strings CSCE150A CSCE150A Without the null terminating character, C would not know where the Introduction Introduction You can use printf to print strings string ends Basics Basics Use %s as a placeholder: Many functions parse a string until it sees ’\0’ String Library String Library printf("My Name is %s.\n", myName); Substrings Substrings Without it, the program would run into memory space that doesn’t Line Scanning Line Scanning printf prints the string until the first null-terminating character belong to it Sorting Sorting Can specify minimum field width, as with e.g. int : char str[20] can only hold 19 characters: at least one character is Command Command Line Line printf("My Name is %20s.\n", myName); reserved for ’\0’ Arguments Arguments A negative field width will left justify instead of right justify In declarations, char myName[] = "Brian" , C automatically inserts Misc Misc the null-terminating character 11 / 51 12 / 51
Arrays of Strings Arrays of Strings CSCE150A CSCE150A We can initialize an array of strings at declaration in the following manner: 1 char month [12][10] = {"January", "February", Introduction One string is an array of characters; an array of strings is a Introduction Basics Basics 2 "March","April", "May", "June", "July", two-dimensional array of characters String Library String Library 3 "August", "September","October", 1 #define NUM_PEOPLE 30 Substrings Substrings 4 "November", "December"}; 2 #define NAME_LEN 25 Line Scanning Line Scanning 3 ... Sorting Sorting Command 4 char names[NUM_PEOPLE ][ NAME_LEN ]; Command As with other arrays, the [12] is optional Line Line Arguments Arguments Why [10] ? Misc names can hold 30 names, each of up to 24 characters long Misc September is the longest string with 9 characters Needs an additional character for the null-terminating character 13 / 51 14 / 51 Reading Strings I Reading Strings II CSCE150A CSCE150A You can use scanf and %s to read strings Note: no & is used Introduction Introduction Basics Basics The array is already represented by a memory address printf("Enter Topic: "); String Library String Library scanf("%s", string_var); Dangerous: the user can put as many characters as they want Substrings Substrings scanf skips leading whitespace characters such as blanks, newlines, If they input more characters than the string can hold: overflow Line Scanning Line Scanning and tabs Sorting Sorting Starting with the first non-whitespace character, scanf copies the Segmentation fault (if you’re lucky), or may not even crash Command Command characters it encounters into successive memory cells of its character Line Line Rest of the program may produce garbage results Arguments array argument Arguments Misc When a whitespace character is reached, scanning stops, and scanf Misc places the null character at the end of the string in its array argument 15 / 51 16 / 51 String Library Functions: Assignment and Substrings String Library CSCE150A CSCE150A The assignment operator, = works for simple data types Introduction Introduction Basics Basics For strings, = only works in the declaration C provides a standard string library String Library String Library Copying 1 Copying char message [30]; Use #include<string.h> Concatenation Concatenation Comparisons 2 message = "Hello!"; ← Illegal Comparisons Length Length Table 9.1 summarizes which functions are provided Substrings Substrings Copy, concatenation, comparison, length, tokenizer, etc. This is because arrays point to a memory location Line Scanning Line Scanning Sorting Sorting Cannot assign arbitrary values to memory pointers Command Command Must use library functions to do so Line Line Arguments Arguments Misc Misc 17 / 51 18 / 51
Recommend
More recommend