读书人

忙充一下,该如何解决

发布时间: 2012-03-26 15:46:56 作者: rapoo

忙充一下
想写COPY字符串的短剧,郁闷的是对c++实在太差,又好长时间冒接触了,哪位仁兄有空帮我补充一下,感激!PS:莫BS我

#include <iostream.h>

using namespace std;

class Str
{
public:
void StringCopy(char *strDestination, char *strSource);
~StringCopy(char *strDestination, char *strSource);
};

int main()
{
char str[20];
StringCopy(str, "hello World ");
cout < <str;
return 0;
}

[解决办法]
class Str
{
public:
void StringCopy(char *strDestination, char *strSource);
};

void Str::StringCopy(char *strDestination, char *strSource)
{
do
*strDestination++=*strSource++;
while(*strSource!= '\0 ');
*strDestination= '\0 ';
}

int main()
{
char str[20];
Str s;
s.StringCopy(str, "hello World ");
cout < <str;
return 0;
}

[解决办法]
还要注意判断*strSource和*strDestination是否为NULL。StringCopy函数里可以用一句while就可以。
while((*strDestination++ = *strSource++) != '\0 ');

读书人网 >C++

热点推荐