/**************************************************************************** * copy2.c * * Computer Science 50 * David J. Malan * * Copies a string. * * Demonstrates strings as pointers to arrays. ***************************************************************************/ #include #include #include #include #include int main(int argc, char *argv[]) { // get line of text printf("Say something: "); char *s1 = GetString(); if (s1 == NULL) return 1; // allocate enough space for copy char *s2 = malloc((strlen(s1) + 1) * sizeof(char)); if (s2 == NULL) return 1; // copy string int n = strlen(s1); for (int i = 0; i < n; i++) s2[i] = s1[i]; s2[n] = '\0'; // change copy printf("Capitalizing copy...\n"); if (strlen(s2) > 0) s2[0] = toupper(s2[0]); // print original and copy printf("Original: %s\n", s1); printf("Copy: %s\n", s2); // free memory free(s1); free(s2); }