额.....好晕被这个题搞的.......

作者在 2011-06-11 08:05:10 发布以下内容
昨天看的道题目,试了下,两个程序,第一个貌似正常,结果输出数组越界,第二个尝试稍微改动,结果程序崩溃....求助....
已知 strcpy函数的原型是 char *strcpy (char *strDest,const char *strSrc);
其中strDest是目的字符串  strSrc是源字符串,不能调用C++/C的字符串库函数
写出strcpy函数???
char *strcpy (char *strDest,const char *strSrc)
上面是题目

#include<iostream>
using namespace std;
char *strcpy (char *strDest,const char *strSrc);
int main()
{
    char *s1="hello word";
    char*s2=new char[strlen(s1)+1];
    strcpy(s2,s1);
    cout<<s1<<endl;
    cout<<s2<<endl;
    system("pause");
    return 0;
}
char *strcpy (char *strDest,const char *strSrc)
{
     int i;
     for(i=0;i<strlen(strSrc);i++)
     strDest[i]=strSrc[i];
     return strDest; 
}
输出来的结果:hello word
hello wordrosoftP葑勳O
请按任意键继续. . .求解释




改动后:




#include<iostream>
using namespace std;
char *strcpy (char *strDest,const char *strSrc);
int main()
{
    char *s1;
    cin>>s1;
    for(int j=0;j<strlen(s1);j++)
    cout<<s1[j];
    char*s2=new char[strlen(s1)+1];
    strcpy(s2,s1);
    cout<<s2<<endl;
    system("pause");
    return 0;
}
char *strcpy (char *strDest,const char *strSrc)
{
     int i;
     for(i=0;i<strlen(strSrc);i++)
     strDest[i]=strSrc[i];
     return strDest; 
}
程序崩溃.....求助.....
搜索更多
默认分类 | 阅读 1266 次
文章评论,共8条
zhongjiezhe
2011-06-15 17:37
1
#include&lt;iostream.h&gt;<br />
#include&lt;string.h&gt;<br />
#define N 100<br />
char *strcopy(char *strDest,const char *strSrc); <br />
int main()<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; char a[N];<br />
&nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;input a string&quot;&lt;&lt;endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cin.getline(a,N);<br />
&nbsp; &nbsp; &nbsp; &nbsp; char *b,*c;<br />
&nbsp; &nbsp; &nbsp; &nbsp; c=a;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;c&lt;&lt;endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; b=new char [N];<br />
&nbsp; &nbsp; &nbsp; &nbsp; b=strcopy(b,c);<br />
&nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;b&lt;&lt;endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; delete(b);<br />
&nbsp; &nbsp; &nbsp; &nbsp; system(&quot;pause&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; return 0;<br />
}<br />
char *strcopy (char *strDest,const char *strSrc)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; int n;<br />
&nbsp; &nbsp; &nbsp; &nbsp; n=strlen(strSrc);<br />
&nbsp; &nbsp; &nbsp; &nbsp; int k;<br />
&nbsp; &nbsp; &nbsp; &nbsp; k=n;<br />
&nbsp; &nbsp; &nbsp; &nbsp; while(k--)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *strDest++=*strSrc++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; *strDest=NULL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; strDest=strDest-n;<br />
&nbsp; &nbsp; &nbsp; &nbsp; return strDest;<br />
}
lianjiecuowu(作者)
2011-06-15 18:01
2
<div class="quote"><span class="q"><b>zhongjiezhe</b>: #include&lt;iostream.h&gt;<br />
#include&lt;string.h&gt;<br />
#define N 100<br />
char *strcopy(char *strDest,const char *strSrc); <br />
int main()<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; char a[N];<br />
&nbsp; &nbsp; &nbsp; &nbsp; cout&lt</span></div><img src="image/face/4.gif" class="face">姜还是老的辣~~~3q
麦穗。夏
2011-06-16 22:32
3
<div class="quote"><span class="q"><b>zhongjiezhe</b>: #include&lt;iostream.h&gt;<br />
#include&lt;string.h&gt;<br />
#define N 100<br />
char *strcopy(char *strDest,const char *strSrc); <br />
int main()<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; char a[N];<br />
&nbsp; &nbsp; &nbsp; &nbsp; cout&lt</span></div><img src="image/face/12.gif" class="face">我怎么执行不出来,有错误啊!!!
lianjiecuowu(作者)
2011-06-17 08:13
4
<div class="quote"><span class="q"><b>麦穗。夏</b>: <img src="image/face/12.gif" class="face">我怎么执行不出来,有错误啊!!!</span></div>#include&lt;iostream&gt;<br />
#include&lt;string&gt;<br />
using namespace std;<br />
#define N 100<br />
char *strcopy(char *strDest,const char *strSrc); <br />
int main()<br />
{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;char a[N];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;input a string&quot;&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cin.getline(a,N);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;char *b,*c;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;c=a;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cout&lt;&lt;c&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;b=new char [N];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;b=strcopy(b,c);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cout&lt;&lt;b&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;delete(b);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;system(&quot;pause&quot;);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return 0;<br />
}<br />
char *strcopy (char *strDest,const char *strSrc)<br />
{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int n;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;n=strlen(strSrc);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int k;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k=n;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while(k--)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; *strDest++=*strSrc++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*strDest=NULL;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;strDest=strDest-n;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return strDest;<br />
}<br />
<br />
<br />
.h是c语言的标砖,而system(&quot;pause&quot;)要用c++的标准,即using namespace std;你再试试阿。这样就不会提示错误啦,嘿嘿
光明
2011-06-17 09:33
5
复制玩最后一个字符之后要在dest后加一个\n字符
ebony
2011-06-22 03:27
6
#include&lt;iostream&gt;<br />
using namespace std;<br />
<br />
char *strcpy (char *strDest,const char *strSrc);<br />
<br />
int main()<br />
{<br />
&nbsp; &nbsp; char *s1 = &quot;hello word&quot;;<br />
&nbsp; &nbsp; char *s2 = new char[strlen(s1)+1];<br />
&nbsp; &nbsp; &nbsp; &nbsp; char i = strlen(s1);<br />
<br />
&nbsp; &nbsp; strcpy(s2,s1);<br />
<br />
&nbsp; &nbsp; cout&lt;&lt;s1&lt;&lt;endl;<br />
&nbsp; &nbsp; cout&lt;&lt;s2&lt;&lt;endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; delete []s2;<br />
<br />
&nbsp; &nbsp; system(&quot;pause&quot;);<br />
<br />
&nbsp; &nbsp; return 0;<br />
}<br />
<br />
char *strcpy (char *strDest,const char *strSrc)<br />
{<br />
&nbsp; &nbsp;&nbsp;&nbsp;int i;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;int j = 0;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;j = strlen(strSrc);<br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;for(i=0; i&lt;j; i++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strDest<i> = strSrc<i>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;strDest<i> = '\0';<br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;return strDest; <br />
}
ebony
2011-06-22 03:27
7
strDest<i> = '\0';
ebony
2011-06-22 03:28
8
数组显示不出来, STRDEST[I] = '\0';
游客请输入验证码