Advertisement



< Prev
Next >



C++ strcpy() function




The strcpy() is a string manipulation function defined in the cstring header file, which works on a string which is stored in a c-style char array and this function 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<iostream>
#include<cstring>

using namespace std;

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

cout<< "The value in dest string : " << dest << "\n";

cout<< "The second in src string : " << src << "\n";

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

cout<< "Length of characters in dest string : " << len1 << "\n";
cout<< "Length of characters in src string : " << len2 << "\n";

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'; 


cout<< "Copying src string into dest is done" << "\n";
cout<< "The value in dest string : " << dest << "\n";
cout<< "The second in src string : " << 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 -





< Prev
Next >
< C++ strncat() function
C++ strncpy() function >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement