computer science engineering 150a
play

Computer Science & Engineering 150A Introduction Problem - PowerPoint PPT Presentation

CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Basics String Library Lecture 07 - Strings Substrings Line Scanning Sorting Stephen Scott Command Line (Adapted from Christopher M. Bourke)


  1. CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Basics String Library Lecture 07 - Strings Substrings Line Scanning Sorting Stephen Scott Command Line (Adapted from Christopher M. Bourke) Arguments Misc Fall 2009 1 / 51

  2. Chapter 9 CSCE150A Introduction 9.1 String Basics Basics 9.2 String Library Functions: Assignment and Substrings String Library 9.3 Longer Strings: Concatenation and Whole-Line Input Substrings Line Scanning 9.4 String Comparison Sorting 9.6 Character Operations Command Line 9.7 String-to-Number and Number-to-String Conversion Arguments Misc 9.8 Common Programming Errors 2 / 51

  3. Strings CSCE150A Introduction Basics Until now we have only dealt with single characters String Library Substrings char myChar = ’A’ , ’\n’ Line Scanning Processing and manipulating single characters is too limiting Sorting Need a way for dealing with groups of characters Command Line Arguments Misc 3 / 51

  4. Strings CSCE150A Introduction A collection of characters is called a string Basics String Library C has no string data type Substrings Instead, strings are arrays of characters, char myString[] , Line Scanning char myName[20] Sorting Necessary to represent textual data, communicate with users in a Command Line readable manner Arguments Misc 4 / 51

  5. String Basics CSCE150A Introduction Calls to scanf or printf used a string constant as the first Basics argument. String Library We have also dealt with static strings : "Hello World!" Substrings printf("a = %d\n", a) Line Scanning printf("Average = %.2f", avg) Sorting Each string above is a string of 12, 7, and 14 characters, respectively Command Line Arguments It’s possible to use a preprocessor directive: Misc #define INSUFF_DATA "Insufficient Data" 5 / 51

  6. Static Strings CSCE150A Introduction Basics Static strings cannot be changed during the execution of the program String Library Substrings They cannot be manipulated or processed Line Scanning May only be changed by recompiling Sorting Stored in an array of a fixed size Command Line Arguments Misc 6 / 51

  7. Declaring and Initializing String Variables CSCE150A Strings are character arrays Declaration is the same, just use char Introduction char string_var[100]; Basics char myName[30]; String Library myName will hold strings anywhere from 0 to 29 characters long Substrings Individual characters can be accessed/set using indices Line Scanning Sorting 1 myName [0] = ’B’; Command Line 2 myName [1] = ’r’; Arguments 3 myName [2] = ’i’; Misc 4 myName [3] = ’a’; 5 myName [4] = ’n’; 6 printf("First initial: %c.\n", myName [0]); 7 / 51

  8. Declaring and Initializing String Variables CSCE150A Introduction You can declare and initialize in one line Basics Be sure to use the double quotes String Library char myName[30] = "Brian"; Substrings Line Scanning You need not specify the size of the array when declaring-initializing Sorting in one line: Command Line char myName[] = "Brian"; Arguments C will create a character array large enough to hold the string Misc 8 / 51

  9. Null Terminating Character CSCE150A Introduction C needs a way to tell where the end of a string is Basics String Library With arrays, it is your responsibility to ensure you do not access Substrings memory outside the array Line Scanning To determine where the string ends, C uses the null-terminating Sorting character : ’\0’ Command Line Character with ASCII code 0 Arguments Misc 9 / 51

  10. Null Terminating Character Example CSCE150A Introduction char str[20] = "Initial value"; will produce the following in Basics memory: String Library Substrings [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Line Scanning I n i t i a l v a Sorting [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] Command Line ? ? ? ? ? ? l u e \0 Arguments Misc 10 / 51

  11. Arrays of Strings CSCE150A Without the null terminating character, C would not know where the Introduction string ends Basics Many functions parse a string until it sees ’\0’ String Library Substrings Without it, the program would run into memory space that doesn’t Line Scanning belong to it Sorting char str[20] can only hold 19 characters: at least one character is Command Line reserved for ’\0’ Arguments In declarations, char myName[] = "Brian" , C automatically inserts Misc the null-terminating character 11 / 51

  12. Printing Strings CSCE150A Introduction You can use printf to print strings Basics Use %s as a placeholder: String Library printf("My Name is %s.\n", myName); Substrings Line Scanning printf prints the string until the first null-terminating character Sorting Can specify minimum field width, as with e.g. int : Command printf("My Name is %20s.\n", myName); Line Arguments A negative field width will left justify instead of right justify Misc 12 / 51

  13. Arrays of Strings CSCE150A Introduction One string is an array of characters; an array of strings is a Basics two-dimensional array of characters String Library 1 #define NUM_PEOPLE 30 Substrings 2 #define NAME_LEN 25 Line Scanning 3 ... Sorting 4 Command char names[NUM_PEOPLE ][ NAME_LEN ]; Line Arguments Misc names can hold 30 names, each of up to 24 characters long 13 / 51

  14. Arrays of Strings CSCE150A We can initialize an array of strings at declaration in the following manner: 1 char month [12][10] = {"January", "February", Introduction 2 Basics "March","April", "May", "June", "July", String Library 3 "August", "September","October", Substrings 4 "November", "December"}; Line Scanning Sorting Command As with other arrays, the [12] is optional Line Arguments Why [10] ? Misc September is the longest string with 9 characters Needs an additional character for the null-terminating character 14 / 51

  15. Reading Strings I CSCE150A You can use scanf and %s to read strings Introduction Basics printf("Enter Topic: "); String Library scanf("%s", string_var); Substrings scanf skips leading whitespace characters such as blanks, newlines, Line Scanning and tabs Sorting Starting with the first non-whitespace character, scanf copies the Command characters it encounters into successive memory cells of its character Line Arguments array argument Misc When a whitespace character is reached, scanning stops, and scanf places the null character at the end of the string in its array argument 15 / 51

  16. Reading Strings II CSCE150A Note: no & is used Introduction Basics The array is already represented by a memory address String Library Dangerous: the user can put as many characters as they want Substrings If they input more characters than the string can hold: overflow Line Scanning Sorting Segmentation fault (if you’re lucky), or may not even crash Command Line Rest of the program may produce garbage results Arguments Misc 16 / 51

  17. String Library Functions: Assignment and Substrings CSCE150A The assignment operator, = works for simple data types Introduction Basics For strings, = only works in the declaration String Library Copying 1 char message [30]; Concatenation Comparisons 2 message = "Hello!"; ← Illegal Length Substrings This is because arrays point to a memory location Line Scanning Sorting Cannot assign arbitrary values to memory pointers Command Line Must use library functions to do so Arguments Misc 17 / 51

  18. String Library CSCE150A Introduction Basics C provides a standard string library String Library Copying Use #include<string.h> Concatenation Comparisons Length Table 9.1 summarizes which functions are provided Substrings Copy, concatenation, comparison, length, tokenizer, etc. Line Scanning Sorting Command Line Arguments Misc 18 / 51

  19. String Assignment I CSCE150A To assign a value to a string, we actually copy it Introduction char *strcpy(char *dest, const char *src) copies string src Basics String Library (source) into dest (destination) Copying Concatenation Note: Comparisons Length Second argument has the keyword const : guarantees the source Substrings string is not modified Line Scanning First argument must point to a memory location large enough to Sorting handle the size of dest Command This is your responsibility; C does not do it for you Line Arguments Returns a pointer to the first character of dest Misc 19 / 51

  20. String Assignment II CSCE150A 1 char myEmail [30]; Introduction 2 strcpy(myEmail , "bgriffin@cse .unl.edu"); Basics String Library Copying Be very careful: Concatenation Comparisons Length 1 char myEmail [10]; Substrings 2 strcpy(myEmail , "bgriffin@cse .unl.edu"); Line Scanning Sorting In this case, se.unl.edu would overwrite adjacent memory cells Command Line Arguments Misc 20 / 51

Recommend


More recommend