Write a program that reads two strings CH1 and CH2 and copies the first half of CH1 and the first half of CH2 into a third CH3. Show the result.
Use the special functions of the string library.
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <string.h>
void main()
{
char CH1[100], CH2[100]; /* given strings */
char CH3[100]=""; /* resulting string */
/* Reading */
printf("Enter the first string : ");
gets(CH1);
printf("Enter the second string : ");
gets(CH2);
strncpy(CH3, CH1, strlen(CH1)/2);
strncat(CH3, CH2, strlen(CH2)/2);
/* Displaying the result */
printf("Half of \"%s\" plus half of \"%s\" gives \"%s\"\n", CH1, CH2, CH3);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Leaf nodes in binary trees