Home / Programs / Write a program that uses a function to copy one string into another without using the strcpy() function available in the standard library of C.
Programming Example

Write a program that uses a function to copy one string into another without using the strcpy() function available in the standard library of C.

👁 889 Views
💻 Practical Program
📘 Step by Step Learning
Write a program that uses a function to copy one string into another without using the strcpy() function available in the standard library of C.

Program Code

 #include <stdio.h>
void string_copy(char [], char []); /* function prototype */


int main()
{
	char a[100]; /*** source string ***/
	char b[100]; /*** destination string ***/
	printf("\n Input source string : ");
	scanf("%[^\n]",a); /* read input source string */
	string_copy(b,a); /* function call */
	printf("\n Destination string : %s\n",b);
	return 0;
}
/*** function definition ***/
void string_copy(char d[], char s[])
{
	int i = 0;
	printf("\n Source string : %s\n",s);
	/* copying the string */
	for (i = 0; s[i] != '\0'; i++)
			d[i] = s[i];
	d[i] = s[i]; /* Copy NUL character to destination string */
}
 

Output

 Input source string : atnyla

 Source string : atnyla

 Destination string : atnyla

 

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.