C PROGRAMMING PROGRAM • STRINGS
Concatenate Two Strings Without strcat()
Learn how to concatenate two strings without strcat() using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
Code Bhavya
Sample output
Combined: CodeBhavya
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <string.h>
int main(void)
{
char first[400], second[200];
int firstLength = 0, secondIndex = 0;
printf("Enter first text: ");
fgets(first, 200, stdin);
first[strcspn(first, "\n")] = '\0';
printf("Enter second text: ");
fgets(second, sizeof second, stdin);
while (first[firstLength] != '\0') firstLength++;
while (second[secondIndex] != '\0') first[firstLength++] = second[secondIndex++];
first[firstLength] = '\0';
printf("Combined: %s", first);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Combined: CodeBhavya
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Find the end of the first string and append characters from the second string.
- Display the computed result.
Find the end of the first string and append characters from the second string.
EFFICIENCY
Time and space complexity
Time complexity
O(n+m)
Auxiliary space
O(n+m)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
