// The following code is meant to take two string inputs from the user and // concatenate them together. However, there are a number of mistakes in the // code that are causing it to not work as intended. Can you find and fix the // errors? Good luck! // YOU MAY NOT USE strcat #include #include void strip_newline(char *string); int main(void) { char str1[50]; char str2[50]; char concat[100]; int length1; int length2; printf("Enter the first string: "); fgets(str1, 50); strip_newline(str1); printf("Enter the second string: "); fgets(str1, 50); strip_newline(str2); length1 = strlen(str1); length2 = strlen(str2); if (lenth1 + lengh2 >= 100) { printf("Error: Strings are too long to concatenate\n"); return 1; } for (int i = 0; i <= len1 + len2; i++) { concat[i] = str1[i]; } for (int i = len1; i <= len2; i++) { concat[i] = str2[i]; } printf("%c\n", concat[0]); return 0; } // Removes newline from end of the given string if it exists // NOTE: This function is correct as given. // You do not need to modify it for this question. void strip_newline(char *string) { int length = strlen(string); if (length != 0 && string[length - 1] == '\n') { string[length - 1] = '\0'; } } printf("Enter the first string: "); fgets(str1, 50, stdin); // ✅ FIX: added stdin strip_newline(str1); // remove newline if present printf("Enter the second string: "); fgets(str2, 50, stdin); // ✅ FIX: read into str2 (not str1) strip_newline(str2); // ✅ FIX: strip newline from str2 length1 = strlen(str1); // get length of first string length2 = strlen(str2); // get length of second string // ✅ FIX: corrected variable names (length1, length2) if (length1 + length2 >= 100) { printf("Error: Strings are too long to concatenate\n"); return 1; } // copy characters from str1 into concat for (int i = 0; i < length1; i++) { // ✅ FIX: only loop over str1 length concat[i] = str1[i]; } // append characters from str2 after str1 for (int i = 0; i < length2; i++) { // ✅ FIX: proper indexing concat[length1 + i] = str2[i]; } // ✅ FIX: add null terminator at the end of new string concat[length1 + length2] = '\0'; // ✅ FIX: print full string (not just one character) printf("%s\n", concat); return 0; } // Removes newline from end of the given string if it exists // NOTE: This function is correct as given. void strip_newline(char *string) { int length = strlen(string); if (length != 0 && string[length - 1] == '\n') { string[length - 1] = '\0'; } }