how to use strcpy in c code example
Example 1: c copy string
char *str = "Hello World!";
char copy[64]; // or 'char *copy = malloc(strlen(str) + 1);'
strcpy(copy, str);
Example 2: strcpy
/* strcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);//str1 copies to str2
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
Example 3: strcpy en c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char Temp[255];
strcpy(&Temp,"Gladir");
strcpy(&Temp,"ABC");
strcpy(&Temp,"Gladir.com");
puts(&Temp);
return 0;
}
Example 4: strcpy c implementation
#include <stdio.h>
// Function to implement strcpy() function
char* strcpy(char* destination, const char* source)
{
// return if no memory is allocated to the destination
if (destination == NULL)
return NULL;
// take a pointer pointing to the beginning of destination string
char *ptr = destination;
// copy the C-string pointed by source into the array
// pointed by destination
while (*source != '\0')
{
*destination = *source;
destination++;
source++;
}
// include the terminating null character
*destination = '\0';
// destination is returned by standard strcpy()
return ptr;
}
// Implement strcpy function in C
int main(void)
{
char source[] = "Techie Delight";
char destination[25];
printf("%s\n", strcpy(destination, source));
return 0;
}
Example 5: strcpy implementation in c
while ((*destination++ = *source++) != '\0');