Advertisement
char * strcpy ( char * destination, const char * source )
// strncpy example
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char src[10] = "Welcome";
char dest[10];
cout<< "The content of source string : " << src << "\n";
strcpy(dest, src);
cout<< "Copying the string is completed" << "\n";
cout<< "The content of source string : " << src << "\n";
cout<< "The content of destination string : " << dest;
return 0;
}
The content of source string : Welcome
Copying the string is completed
The content of source string : Welcome
The content of destination string : Welcome
// strncpy example
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char src[30] = "Be good to each other";
char dest[30] = "Hello";
cout<<"The content of source string : " <<src <<"\n";
strcpy(dest, src);
cout<<"Copying the string is completed" <<"\n";
cout<<"The content of source string : " <<src <<"\n";
cout<<"The content of target string : " <<dest;
return 0;
}
The content of source string : Be good to each other
Copying the string is completed
The content of source string : Be good to each other
The content of target string : Be good to each other
Advertisement
// 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;
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement