09SortingSmall

09SortingSmall - 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 s Definition s An array of characters s Where the used portion is terminated by a null character s <string.h> s Library that acts on C-strings s Most will crash if given something that does not fit the definition above s Creating and Initializing a string char name1[4] = {‘J’,’a’,’n’,’\0’}; char name2[6] = “Plane”; s Characters, strings and numeric values are all different length of the string and the sizeof operator s sizeof operator tells the size of the variable or type s strlen uses the definition of C-string to find number of used characters 0 ' 0 ' " 0 " Input and Output s Output s %s in printf format string s puts() function takes a string as the only argument s Input s dangerous to use %s in scanf or to use gets() function s char *fgets(char *buffer, int bufferSize, FILE *stream); s read a line into buffer (at most bufferSize-1 characters) s null byte added at end of buffer s reads from stream – for standard input just type stdin as the name of the stream s returns NULL on error or end of file s on success returns pointer to the space where you read into (here called the buffer)
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Strings s Zero or more characters followed by null char ‘\0’ s also called NUL s not counted as part of string s string.h defines prototypes for string routines s Some String Functions s size_t strlen(char const *str); s returns count of characters in str s up to but not including the null character s char *strncpy(char *dst, char const *src, size_t len); s copy src to dst (a better version of strcpy) s copy until ‘\0’ in src or at most len characters s pad extra characters will ‘\0’ s Safety tip: dst[len-1] = '\0'; to force termination of new string s char *strncat(char *dst, char const *src, size_t len);
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.

Page1 / 6

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

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

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