c++実装文字列レプリケーション関数strcpy()

677 ワード

#include<iostream>
#include<stdlib.h>
using namespace std;

char *str_cpy(char *dest,char s[])
{
	char *p=s;
	char *q=dest;
	int m=strlen(s);
	while(m--!=0)
	{
		*q++=*p++;
	}
	*q='\0';
	return dest;
}
int main()
{
	char a[100];
	cout<<"please input a:";
	gets_s(a);
	cout<<"a:"<<a<<endl;

	char *p=(char*)malloc(strlen(a)+1);
	str_cpy(p,a);
	cout<<"p:"<<p<<endl;
	system("pause");
	return 0;
}

簡単で乱暴!