/** * copy-1.c * * David J. Malan * malan@harvard.edu * * Copies a string. * * Demonstrates strings as pointers to arrays. */ #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[0] = toupper(t[0]); } // print original and copy printf("Original: %s\n", s); printf("Copy: %s\n", t); // free memory free(s); free(t); // success return 0; }