We earlier learned about strings in C programming in our Strings in C tutorial. Today, we will focus on strcpy() in C programming.

The strcpy() function in C Programming

The strcpy() function copies the contents of one string into another. The base address of the target and the source(or the source string) should be supplied to this function in order to copy the source string into the target one. Let’s look at the syntax,

strcpy( char *target, const char *source );

strcpy() goes on copying the characters from the source string into the target string until it encounters the Null character(‘’). Since the string gets copied in a character-by-character fashion. It should be kept in mind that the target string should have a dimension that can hold the copied string.

How to use the strcpy() function in C

The given example below illustrates the use of strcpy() function in C,

#include<stdio.h>
#include<string.h>
int main()
{
	char target[30],source[30];
	printf("Enter the source string to be copied:");
	fgets(source,30,stdin);
	printf("nnsource---->targetnn");
	strcpy(target,source);
	printf("The target after copying is:%s",target);
	return 0;
}

Output:

strcpy() in C programming
  • In the code above, we first declare the two target and source strings.
  • After that, user input for the source string is taken. Which in this case was “JournalDev strcpy() example”.
  • When we have the source string, we used the strcpy() function from the string.h header file. The function copied the contents of the source string into the target one.

Please note that the source string could also be passed directly to the strcpy() function. But for the target string, we always need to specify the pointer pointing to it.

Making our own string copying function

Let us attempt at making our own string copying function for a better understanding of how the strcpy() function works.

#include<stdio.h>
char *My_strcpy(char *t, const char *s)  //My_strcpy() definition
{
	char *temp;
	temp=t;
	while(*s!='')       //character-by-charaacter copying
	{
		*t=*s;
		s++;
		t++;
	}
	*t="";    //string termination
	return temp;
}
int main()
{
	char target[30],source[30];
	printf("Enter the source string to be copied:");
	fgets(source,30,stdin);
	printf("nnsource---->targetnn");
	My_strcpy(target,source);
	printf("The target after copying is:%s",target);
	return 0;
}

Output:

My_strcpy-output

My_strcpy() Output

Above we declare as well as define My_strcpy() function before the main() function. And inside main() we use it instead of the pre-defined strcpy() function to get the above output.

Did you notice while defining the My_strcpy() function we initialized pointer *s to be constant? This was done to ensure that by any means the function doesn’t alter the contents of the source string.

This is how the function strcpy() works in C programming.

Conclusion

In this tutorial, we first learned what is the strcpy() function. We also learned how to implement our own strcpy() function through the implementation of My_strcpy().

For further reading – Check all the previous tutorials on C Programming on Journaldev.

References

By admin

Leave a Reply

%d bloggers like this: