strncpy
()属于
strcpy
()的扩展,使用时需要包含
头文件
#include <
string.h
>.
其 函数原型 为 strncpy (char *s1,const char *s2,int n);其中有三个参数分别表示目标字符串s1,源字符串s2,拷贝长度。意思是将s2指向的字符串的前n个长度的字符放到s1指向的字符串中,并将s1原有的前n个字符覆盖.
例子:
其 函数原型 为 strncpy (char *s1,const char *s2,int n);其中有三个参数分别表示目标字符串s1,源字符串s2,拷贝长度。意思是将s2指向的字符串的前n个长度的字符放到s1指向的字符串中,并将s1原有的前n个字符覆盖.
例子:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s1[100];
char s2[100];
cin>>s1>>s2;
cout<<s1<<" "<<s2<<endl;
strncpy(s1,s2,3);
cout<<s1<<" "<<s2<<endl;
return 0;
}