use strcat, strncpy, stpcpy, sprintf, strlen+memcpy, etc
In short, you don't. strncpy is deemed unsafe as it has potential to cause buffer overruns. To copy strings safely in C++, use std::string instead. For examples and syntax, see related links, below.
memcpy is general purpose copy. and strcpy is specific for string copying. strcpy will copy the source string to destination string and terminate it with '\0' character but memcpy takes extra argument which specifies the number of bytes to copy.memcpy will not handle copying of overlapping memory. use memove instead.
Using strcpy and strcat. Or sprintf. Or strlen+memcpy. There are more than solutions.
They do different things, so they are uncomparable.PS: strcpy can be implemented with strlen+memcpy:char *strcpy (char *dest, const char *src){size_t len;len= strlen (src);memcpy (dest, src, len);return dest;}
#include <stdio.h> #include <string.h> void main() { char a[4][25],temp[25]; int i,j; clrscr(); printf("Enter the names\n"); for (i=0;i<4;i++) gets(a[i]); for (i=0;i<3;i++) for (j=i+1;j<4;j++) { if (strcmp(a[i],a[j])>0) { strcpy(temp,a[i]); strcpy(a[i],a[j]); strcpy(a[j],temp); } } printf("Sorted strings are \n"); for (i=0;i<4;i++) puts (a[i]); getch(); }
strcpy
char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }
i want a coding of a program of a calculator using graphics in c language??
strcpy - copy a string to a location YOU created (you create the location, make sure that the source string will have enough room there and afterwards use strcpy to copy) strdup - copy a string to a location that will be created by the function. The function will allocate space, make sure that your string will fit there and copy the string. Will return a pointer to the created area.
/* this is the same functionality as strcpy() */ /* note that this, like strcpy, is not buffer overrun safe */ char *StringCopy (char *destination, const char *source) { const char *temp = destination; while ((*destination++ = *source++) != '\0'); return temp; }
As usual, you should check official documentation before you ask a question like this. string.h // Copies num characters from source into destination. char* strncpy (char* destination, const char* source, size_t num); // Copies characters from source into destination. char* strcpy (char* destination, const char* source);