strcpy(char destination[], const char source[]);
strcpy:将字符串source拷贝到字符串destination中。
strcpy函数应用举例
原型:strcpy(char destination[], const char source[]);
功能:将字符串source拷贝到字符串destination中
例程:
#include <iostream.h>
#include <string.h>
void main(void)
{
char str1[10] = { "TsinghuaOK"};
char str2[10] = { "Computer"};
cout <<strcpy(str1,str2)<<endl;
}
运行结果是:Computer
第二个字符串将覆盖掉第一个字符串的所有内容!
注意:在定义数组时,字符数组1的字符串长度必须大于或等于字符串2的字符串长度。不能用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。所有字符串处理函数都包含在头文件string.h中。
strncpy(char destination[], const char source[], int numchars);
strncpy:将字符串source中前numchars个字符拷贝到字符串destination中。
strncpy函数应用举例
原型:strncpy(char destination[], const char source[], int numchars);
功能:将字符串source中前numchars个字符拷贝到字符串destination中
例程:
#include <iostream.h>
#include <string.h>
void main(void)
{
char str1[10] = { "Tsinghua "};
char str2[10] = { "Computer"};
cout <<strncpy(str1,str2,3)<<endl;
}
运行结果:Comnghua
注意:字符串source中前numchars个字符将覆盖掉字符串destination中前numchars个字符!