CMPT 125 Lecture 3: L3 P2
Document Summary
Example: char *word1 = hello ; char word2[6] = { h", e", l", l", o", \0"}; // length of string = 6 printf ( %s \n , word1); // prints hello printf ( %s \n , word2); // prints hello. Word1 - initializing with hello , creates an array of const chars immutable strings - cannot be changed. This allows the compiler to optimize the code. Word2 - can be modified, e. g. word2[3] = p"; word2[4] = \0"; printf ( %s \n , word2); // prints help. String. h - two useful functions int strlen(char s[]) What happens if there is no \0" in the string? char* strcpy(char* dest, char* src) The length of dest must be sufficient to copy src. 5 characters (including \0"). printf(%sn, str2); // prints hell printf(%dn, strlen(str2)); // prints 4 (excluding \0"). String. h - strcat() char* strcat(char* dest, const char* src) Appends src to the end of dest.