幫忙補充一下,该如何解决
幫忙補充一下
想写個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 ');
想写個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 ');