lecture15-feb24 - Announcements - Lecture 15 Assignment 3...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Announcements - Lecture 15 • Assignment 3 – Program due tomorrow – Write down: # statements, #hours, #logic errors Remember char’s?? #include <ctype.h> . . char answer = ‘Y’; . . . answer = getchar ( ); putchar (answer); if (answer == ‘N’) { . . . } else { . . . } Topics for today (Ch. 13) string -> a series/sequence of characters (chars) More than just a 1D array of char’s strings and pointers later Exam Q0 Results highs 257 20 50 lows 68 1 0 med. 150 5 4 SLOCs Hrs. Defects Assignment 2 Data Challenge -continue collecting size, effort, defect data on yourself sloc/hr = 30 defects/ sloc =.03 productivity & quality What do we do with Strings? • Input and output them • Make a bigger String out of little ones • Break big Strings into smaller ones • Do comparisons (e.g. alphabetical ordering) • Extremely useful in any application that manipulates text (e.g. translators, word processors, language puzzles, etc.) String Literals • String literals are enclosed in double quotes; e.g.: "Put a disk in drive A, then press any key to continue\n" • A string literal may be extended over more than one line by writing \ immediately followed by the end of the line: printf("Put a disk in drive A, then \ press any key to continue\n"); • A string literal may be divided into two or more shorter strings; the compiler will join these together into one string: printf("Put a disk in drive A, then " "press any key to continue\n"); How String Literals Are Stored • The string literal "abc" is represented by the three characters a, b, and c, followed by a null character ( \0 ): • Like any array, a string literal is represented by a pointer to the first character in the string. • A string literal of length 1 is different from a character constant – A string literal of length 1 ("a", for example) is represented by a pointer – A character constant ('a', for example) is represented by an integer value – Don’t use a character constant when a string literal is required (or vice-versa). The call printf('\n'); is not legal. a b c \0 String Variables • A string variable is defined as a 1D array of chars, e.g.: #define STR_LEN 80 char str [STR_LEN+1]; The array should be one character longer than the string it will hold, to leave space for the null character at the end. • Leave room for the null character when using string- handling functions in the C library. • A string variable can be initialized in the declaration: char date1[8] = "June 14"; • A string initializer need not completely fill the array: char date2[9] = "June 14"; The leftover array elements are filled with null characters: • If the length of the array is omitted, the compiler will compute it: char date3[ ] = "June 14 "; /* date3 is 9 characters long */
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

lecture15-feb24 - Announcements - Lecture 15 Assignment 3...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online