// We did not go through this example in the lecture // It combines 2 strings together with a space in the middle // and shows the use of strncat // strncat will make #include #include #define MAX_LEN 10 void remove_trailing_nl(char s[]); int main(void) { // All initialised to empty strings; // They will be arrays filled with '\0' characters char first_name[MAX_LEN] = ""; char last_name[MAX_LEN] = ""; char full_name[MAX_LEN * 2] = ""; // Read first name and last name printf("Enter first name: "); fgets(first_name, MAX_LEN, stdin); remove_trailing_nl(first_name); printf("Enter last name: "); fgets(last_name, MAX_LEN, stdin); remove_trailing_nl(last_name); // Manually concatenate first name and last name with a space in between // Append first_name to full_name. strncat(full_name, first_name, MAX_LEN * 2 - 1); // Add space character strncat(full_name, " ", MAX_LEN * 2 - strlen(full_name) - 1); // Append last_name strncat(full_name, last_name, MAX_LEN * 2 - strlen(full_name) - 1); // Output the full name printf("Full name: %s\n", full_name); return 0; } // Remove the trailing newline character of the string // if it exists void remove_trailing_nl(char s[]) { int len = strlen(s); if (s[len - 1] == '\n') { s[len - 1] = '\0'; } }