Write a program that reads two strings STR1 and STR2, then appends them in a third string STR3 without using any predefined function from the string.h library.
Each of the 3 strings can contain 20 characters at maximum.
The program should test if the third string can hold the concatenation.
In the positive case, print STR3. In the negative case, print the error message "Impossible to concatenate both strings.".
Running examples:
Difficulty level
This exercise is mostly suitable for students
#include <conio.h>
#define SIZE 20
void main()
{
char STR1[SIZE],STR2[SIZE], STR3[SIZE];
int i , j , length;
printf("Enter the first string: ");
gets(STR1);
printf("Enter the second string: ");
gets(STR2);
// copy and count both sizes
length=0;
i=0;
while(STR1[i])
{
STR3[length]=STR1[i];
i++;
length++;
}
i=0;
while(STR2[i] && length<SIZE)
{
STR3[length]=STR2[i];
i++;
length++;
}
if(length>=SIZE)
printf("Impossible to concatenate both strings.\n");
else
{
STR3[length] = '\0';
puts(STR3);
}
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sequence values and ranks