#include #include // shows stringcopy, stringcat and stringlength FUNCTIONALITY // we would need to #include if we used // strlen, strcpy and strcat instead of our own versions // myStrlen, myStrcpy and myStrcat // behaves just like strcpy in string library char* myStrcpy(char* dest, char* src){ char* dest0 = dest; while ( (*dest++ = *src++) != '\0'); return dest0; } // behaves just like strcat in string library char* myStrcat(char* dest, char* src){ char* dest0 = dest; while (*dest != '\0') *dest++; // WHY NOT: while(*dest++!='\0'); while ((*dest++ = *src++) != '\0'); return dest0; } // behaves just like strlen in string library int myStrlen(char* src){ int len = 0; while (*src != '\0') { len++; *src++; } return len; } int main() { // a string char *hello = "hello"; // a local array of chars char localStr[30]; // two dynamic array of chars // why do we need the +1? char *dynStr = malloc(myStrlen(hello)+1); char *catStr = malloc(30); // strcpy: dest <- source myStrcpy(localStr, hello); printf("localStr after strcpy: %s, length: %d, NOT 30!\n", localStr, myStrlen(localStr)); myStrcpy(dynStr, "goodbye, uhhh .... "); printf("dynStr after strcpy: %s\n", dynStr); // strcat concatenete dest after source // strcat: starts AT the '\0' delimter of dest and copies source there myStrcat(dynStr, localStr); printf("dynStr after strcat: %s\n", dynStr); }