0
1.5kviews
Explain string function for following operation with example. (1) Copy n char from source string to destination.
1 Answer
0
33views

1) The C library function void *memcpy(void *str1, const void *str2, size_t n) copies n characters from memory area str2 to memory area str1.

Example:

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

int main ()
{
constchar src[50]="All is well";
char dest[50];

   printf("Before memcpy dest = %s\n", dest);
   memcpy(dest, src, 6);
   printf("After memcpy dest = %s\n", dest);

return(0);
}

Output

Before memcpy dest =

After memcpy dest = All is

(2) Joining of two string.

The C library function char *strcat(char *dest, const char *src) appends the string pointed to by src to the end of the string pointed to by dest.

Example

#include<stdio.h>
#include<string.h>
int main ()
{
char src[50], dest[50];
   strcpy(src,"well");
   strcpy(dest,"All is ");
   strcat(dest, src);
   printf("%s", dest);
return(0);
}

Output:

All is well

Please log in to add an answer.