C provides the string library (string.h) for manipulating the string operations like
strcpy,
strlen,
strcmp etc. Here I will give undefined implementation of the each string function. I will start with
strcpy function.
strcpy is one of the most common and regularly used function. Its is used to copy one string into another string making original string as it is. It will be very use full while parsing the strings. The syntax for
strcpy is given below.
char *strcpy(char *dst, char *src);
Syntax is taken from the man page. This function copies all the characters (including null character) from
src to the
dst. first argument for this function is destination string and second argument is the source string. See the sample code snippet for string copy using
strcpy function.
#include<stdio.h>
#include<string.h>
main()
{
char amessage[] = "this is a string";
char newmessage[20];
//copying amessage to newmessage
strcpy(newmessage,amessage);
printf("amessage is '%s'\n",amessage);
printf("newmessage is '%s'\n",newmessage);
}
Output:
amessage is 'this is a string'
newmessageis 'this is a string'
To compile above program successfully, we need to include string.h header as we are using
strcpy string function to copy.
Implementing our own user defined string copy function is the common question regularly asked in general C/C++ interviews. So I am giving different ways (arrays and pointers) of implementing string copy function.
String copy function using arrays:
//using arrays , need to move the string using index
void strcpy_arry(char * dest, char *src)
{
int i=0;
while((dest[i] = src[i])!='\0')
i++;
}
Explanation: Using arrays, it is very easy to copy the string. Just take the index value , copy the character at the index into new array and increment the index and continue this until null or '\0' character occurs.
String copy function using pointers:
Method1:
//using pointers, need to move the position of the pointer
void strcpy_ptr(char *dest, char *src)
{
while((*dest = *src)!='\0')
{
src++;
dest++;
}
}
Explanation: Using pointers copying the string is little bit tricky. As most of you know , accessing the value is asterisk(*), assign the value to the new pointer and increment both the pointers. Continue this until pointer reaches the null or '\0' character.
Method2:
//using pointers, need to move the position of the pointer
void strcpy_ptr_short(char *dest, char *src)
{
while((*dest++ = *src++)!='\0')
;
}
Explanation: Same as the above method1, but the difference is, we can use the incrementing pointer statements with in the if statement.
Main function:
main()
{
char amessage[] = "this is a string";
char newmessage[20];
strcpy_arry(newmessage,amessage);
//strcpy_ptr(newmessage,amessage);
//strcpy_ptr_short(newmessage,amessage);
printf("amessage is '%s'\n",amessage);
printf("newmessage is '%s'\n",newmessage);
}
Output:
amessage is 'this is a string'
newmessage is 'this is a string'
In main I commented remaining function calls. you can un-comment and test all the functions.
Even though
strcpy is use full function , there are some important points to remember before using it.
- strcpy function copies the null character also, so its better allocate extra memory for that if you use any memory allocation.
- check the destination string size and make sure that its enough to store the source string.
- if both source and destination strings are overlapped, this functin is undefined.