< Prev
Next >



C strcpy() method




The strcpy() is a string function defined in the string.h header file, which is used copy the characters of one string to another string.





Let's create a program which copies the characters in one string to another string without using strcpy() function.


/* Program to copy the characters of one string to another string without using strcpy()*/

#include<string.h>
#include<stdio.h>

#include
#include

int main()
{
char dest[20] = "Blue";
char src[10] = "Skies";

printf("The value in dest string : %s", dest);
printf("\n");

printf("The second in src string : %s", src);
printf("\n");

int len1 = strlen(dest);
int len2 = strlen(src);

printf("Length of characters in dest string : %d \n", len1);
printf("Length of characters in src string : %d \n", len2);

int i; 
for(i=0;i<len2; i++)
{
	dest[i] = src[i];
	len1 = len1 + 1 ;
}

/* appending null-character at the end of dest char array when copying finishes */
dest[i]='\0'; 


printf("Copying src string into dest is done");
printf("\n");

printf("The value in dest string : %s", dest);
printf("\n");

printf("The second in src string : %s", src);

return 0;
}


Output is :


The value in dest string : Blue
The second in src string : Skies
Length of characters in dest string : 4
Length of characters in src string : 5
Copying src string into dest is done
The value in dest string : Skies
The second in src string : Skies





Note: - The function strlen returns the total number of characters actually present in the char[] and not the total number of character it can hold.





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< C strncat() function
C strncpy() function >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2