Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Create a program that takes two strings as input and concatenates them. Ensure that the resulting string doesn’t exceed a specified maximum length.

#include <stdio.h>
#include <string.h>

int main() {
// Declare variables
char str1[50], str2[50], result[100];

// Input: Get two strings from the user
printf(“Enter the first string: “);
scanf(“%s”, str1);

printf(“Enter the second string: “);
scanf(“%s”, str2);

// Process: Concatenate strings
if (strlen(str1) + strlen(str2) < sizeof(result)) {
strcpy(result, str1);
strcat(result, str2);
printf(“Concatenated string: %sn”, result);
} else {
printf(“Error: Resulting string exceeds the maximum length.n”);
return 1; // Exit the program with an error code
}

return 0; // Exit the program successfully
}