编写一个函数 实现strncpy一样的功能(图)


#include "stdAfx.h"
#include "ctype.h"
#include "stdlib.h"
#include "string.h"

int main()
{
	char *strata(char s1[],char s2[],int n);

	int i=0;
	char nam1[41]="das";
	char nam2[41]="wo shi yi ge da sha bi";

	strata(nam1,nam2,8);

	while(nam1[i]!='\0')
	{
		putchar(nam1[i]);
		i++;
	}

	putchar('\n');

	system("pause");
	return 0;
}

char *strata(char s1[],char s2[],int n)
{
	int i;
	char *prt=s1;

	if(n>strlen(s2))
		return prt;
	else
	{
		s2[n]='\0';
		for(i=0;i<n;i++)
			s1[i]=s2[i];
	}
}

简化修改

#include "stdAfx.h"
#include "ctype.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"

int main()
{
	char *strata(char s1[],char s2[],int n);

	char nam1[41]="das";
	char nam2[41]="wo shi yi ge da sha bi";

	strata(nam1,nam2,8);

	puts(nam1);
	system("pause");
	return 0;
}

char *strata(char s1[],char s2[],int n)
{
	int i;
	char *prt=s1;

	if(n>strlen(s2))
		return prt;
	else
	{
		s2[n]='\0';
		for(i=0;i<n;i++)
			s1[i]=s2[i];
	}
}


你可能感兴趣的:(System,ini,include)