// StrCopy.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstring>
#include <iostream>
using namespace std;
char *StrCopy(char *destStr, const char *srcStr)
{
char *s = NULL;
int srcLen = 0;
int destLen = 0;
if(NULL == srcStr)
return NULL;
if(NULL != srcStr)
{
srcLen = strlen(srcStr);
if(NULL == destStr)
{
destStr = (char*)malloc(sizeof(char) * (srcLen + 1));
s = destStr;
}
else
{
destLen = strlen(destStr);
if(destLen < srcLen)
{
destStr = (char*)malloc(sizeof(char) * (srcLen + 1));
}
s = destStr;
}
}
while(*srcStr != '\0')
{
*destStr = *srcStr;
destStr++;
srcStr++;
}
*destStr = '\0';
return s;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *srcStr = "ABCDEFGHIJKLMNOPQ DFHFGJGHJ GFHFGJGH dsg ";
char destStr[] = "ABCDEFGHIJKdgdfgPQ DFHFGJGHJ GFHFGJGH dsg dsgdfhfgh";
char *dest = NULL;
dest = StrCopy(dest, destStr);
cout << dest <<endl;
return 0;
}
程序员面试100题(算法)之C/C++实现字符串复制
最新推荐文章于 2024-02-23 15:54:19 发布