#include #include /* need to include for strlen, strcmp, strcpy functions */ #define SIZE 80 int main() { char word1[SIZE + 1] = "apples"; char word2[SIZE + 1] = "ALIGATORS"; int i; /* Demonstration of common mistake without setting '\0' to mark end of string */ printf("\nINCORRECT METHOD TO COPY STRINGS (using for loop):\n\n"); for (i = 0; i < strlen(word1); i++) word2[i] = word1[i]; printf ("Contents of word2 is: %s\n\n", word2); printf("\nBETTER METHOD TO COPY STRINGS (using for loop):\n\n"); for (i = 0; i < strlen(word1); i++) word2[i] = word1[i]; word2[strlen(word1)] = '\0'; printf ("Contents of word2 is: %s\n\n", word2); printf("\nEASIEST METHOD TO COPY STRINGS (using strcpy):\n\n"); strcpy(word2, word1); printf ("Contents of word2 is: %s\n\n", word2); }