lecture15-feb25 - Assignment 3 Program due Friday Topics...

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 Friday • Topics for today – Strings -> a series/sequence of characters (chars) 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 (like in chars) • 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 legal, because printf expects a string as its first parameter, but printf('\n'); is not legal. a b c \0 String Variables • A string variable is a one-dimensional 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: 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 */ Reading and Writing Strings To read or write a string, use scanf or printf with the %s conversion specification: scanf("%s", str); printf("%s", str); scanf skips white space, then reads characters into str until it encounters a
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.

This note was uploaded on 01/24/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 4

lecture15-feb25 - Assignment 3 Program due Friday Topics...

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