{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

09SortingLarge - CMSC 106 Lecture Set#9 More About Arrays...

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

View Full Document Right Arrow Icon
1 CMSC 106 Lecture Set #9 – More About Arrays and Sorting Set Started: Monday, October 31, 2010 C-Strings square6 Definition square6 An array of characters square6 Where the used portion is terminated by a null character square6 <string.h> square6 Library that acts on C-strings square6 Most will crash if given something that does not fit the definition above square6 Creating and Initializing a string char name1[4] = {‘J’,’a’,’n’,’\0’}; char name2[6] = “Plane”; square6 Characters, strings and numeric values are all different length of the string and the sizeof operator square6 sizeof operator tells the size of the variable or type square6 strlen uses the definition of C-string to find number of used characters 0 ' 0 ' " 0 "
Background image of page 1

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

View Full Document Right Arrow Icon
2 Input and Output square6 Output square6 %s in printf format string square6 puts() function takes a string as the only argument square6 Input square6 dangerous to use %s in scanf or to use gets() function square6 char *fgets(char *buffer, int bufferSize, FILE *stream); square6 read a line into buffer (at most bufferSize-1 characters) square6 null byte added at end of buffer square6 reads from stream – for standard input just type stdin as the name of the stream square6 returns NULL on error or end of file square6 on success returns pointer to the space where you read into (here called the buffer) Strings square6 Zero or more characters followed by null char ‘\0’ square6 also called NUL square6 not counted as part of string square6 string.h defines prototypes for string routines square6 Some String Functions square6 size_t strlen(char const *str); square6 returns count of characters in str square6 up to but not including the null character square6 char *strncpy(char *dst, char const *src, size_t len); square6 copy src to dst (a better version of strcpy) square6 copy until ‘\0’ in src or at most len characters square6 pad extra characters will ‘\0’ square6 Safety tip: dst[len-1] = '\0';
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}