Advertisement
char * strncat ( char * destination, const char * source, size_t num )
/* Program to append n number of characters of first string at the end of another string. */
#include<string.h>
#include<stdio.h>
int main()
{
char arr1[10] = "Have a ";
char arr2[10] = "good day!";
printf("The content of first string is : %s", arr1);
printf("\n");
printf("The content of second string is : %s", arr2);
printf("\n");
strncat(arr1,arr2,2); /* only first two characters from arr2 will be appended to the end of arr1 */
printf("The addition of two string is : %s", arr1);
printf("\n");
printf("The content of second string : %s", arr2);
return 0;
}
The content of first string is : Have a
The content of second string is : good day!
The addition of two string in the first string : Have a go
The content of second string : good day!
Advertisement
/* Program to append n, number of characters of one string at the end of another string without strcat() function */
#include<string.h>
#include<stdio.h>
int main()
{
char dest[20] = "Hello";
char src[10] = "World";
printf("The value in dest string : %s", dest);
printf("\n");
printf("The second in src string : %s", src);
printf("\n");
int len1 = strlen(dest);
int len2 = strlen(src);
printf("Length of characters in dest string : %d \n", len1);
int num;
printf("How many characters of the first string you to append : ");
scanf("%d", &num);
for(int i=0;i<num; i++)
{
dest[len1] = src[i];
len1 = len1 + 1 ;
}
dest[len1]='\0';
printf("Concatenated strings : %s ", dest);
return 0;
}
The value in dest string : Hello
The second in src string : World
Length of characters in dest string : 5
Length of characters in src string : 5
How many characters of the first string you to append : 3
Concatenated strings : HelloWor
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement