/** * copy-2.c * * David J. Malan * malan@harvard.edu * * Copies a string. * * Demonstrates pointer arithmetic. */ #include #include #include #include int main(void) { // get line of text printf("Say something: "); char* s = GetString(); if (s == NULL) { return 1; } // allocate enough space for copy char* t = malloc((strlen(s) + 1) * sizeof(char)); if (t == NULL) { return 1; } // copy string, including '\0' at end for (int i = 0, n = strlen(s); i <= n; i++) { *(t + i) = *(s + i); } // change copy printf("Capitalizing copy...\n"); if (strlen(t) > 0) { *t = toupper(*t); } // print original and copy printf("Original: %s\n", s); printf("Copy: %s\n", t); // success return 0; }