CMPT 125 Lecture 3: L3 P2

31 views4 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents