//strncpy函数
#include<stdio.h>
#include<stdlib.h>
char *StrCNopy(char *s,char *t,int n);
int main()
{
char *str1,*str2;int nn;
str1=(char*)malloc(100*sizeof(char));
str2=(char*)malloc(100*sizeof(char));
printf("Please input str1:\n");
scanf("%15s",str1);
printf("Please input str2:\n");
scanf("%15s",str2);
printf("Please input NN:\n");scanf("%d",&nn);
printf("result=%s\n",StrCNopy(str1,str2,nn));
return 0;
}
char *StrCNopy(char *s,char *t,int n)
{
char *ps;
ps=s;
while(*t!='\0' && n-->0)
*s++=*t++;
while(n-->0)
*s++='\0'; //如果t中的字符<n,我们就在s的尾部填充'\0'字符
return ps;
}
#include<stdio.h>
#include<stdlib.h>
char *StrCNopy(char *s,char *t,int n);
int main()
{
char *str1,*str2;int nn;
str1=(char*)malloc(100*sizeof(char));
str2=(char*)malloc(100*sizeof(char));
printf("Please input str1:\n");
scanf("%15s",str1);
printf("Please input str2:\n");
scanf("%15s",str2);
printf("Please input NN:\n");scanf("%d",&nn);
printf("result=%s\n",StrCNopy(str1,str2,nn));
return 0;
}
char *StrCNopy(char *s,char *t,int n)
{
char *ps;
ps=s;
while(*t!='\0' && n-->0)
*s++=*t++;
while(n-->0)
*s++='\0'; //如果t中的字符<n,我们就在s的尾部填充'\0'字符
return ps;
}
[此贴子已经被作者于2007-8-8 7:34:18编辑过]