求助。。。下面这个不知道哪有语法错误

作者在 2011-04-06 15:59:54 发布以下内容
/*作业8-5:输入一字符串,将该字符串中第m个字符开始的全部字符复制成另一个字符串。
 m由用户输入,值小于字符串的长度。要求编写一个函数mcopy(char *s , int m)来完成。
 */n
# include "stdio.h"
# include "string.h"
void mcopy(char *s , int m);
int main(void )
{
 int m;
 char str1[100],str2[100];
 puts("please input a string:");
 gets(str1);
 printf("please input m:");
 scanf("%d",&m);
    mcopy(str1,m);
    return 0;
}
void mcopy(char *s , int m)
{
 char str1[100],str2[100];
 int i,n;
 n=strlen(str1);
 for(i = 0; i < n-m; i++)
 str2[i]=str1[i+m];
 puts(str2);
}
默认分类 | 阅读 835 次
文章评论,共11条
jery2487
2011-04-06 18:27
1
既然有形参s,还要定义数组str1干吗,实参传到函数里来是有用处的
linw1225
2011-04-07 21:44
2
格式跟我们平常用的不一样。但是,数组2复制后是不是应该在最后加一个‘\0’??
死不了的恨
2011-04-13 18:49
3
下面是我帮你修改的程序,你可以参考一下,程序已运行,无错。<br />
# include &quot;stdio.h&quot;<br />
# include &quot;string.h&quot;<br />
void mcopy(char *s , int m);<br />
int main(void )<br />
{<br />
 <br />
&nbsp; &nbsp; &nbsp; &nbsp; int m;<br />
&nbsp; &nbsp; &nbsp; &nbsp; char str1[100];<br />
 &nbsp; &nbsp; &nbsp; &nbsp; puts(&quot;please input a string:&quot;);<br />
 &nbsp; &nbsp; &nbsp; &nbsp; gets(str1);<br />
 &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;please input m:&quot;);<br />
 &nbsp; &nbsp; &nbsp; &nbsp; scanf(&quot;%d&quot;,&amp;m);<br />
&nbsp; &nbsp; mcopy(str1,m);<br />
&nbsp; &nbsp; return 0;<br />
}<br />
void mcopy(char *s , int m)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; int i,n;<br />
&nbsp; &nbsp; &nbsp; &nbsp; char str2[100];<br />
&nbsp; &nbsp; &nbsp; &nbsp; n=strlen(s);<br />
&nbsp; &nbsp; &nbsp; &nbsp; for(i = 0; i &lt; n-m; i++)<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str2<i>=*(s+(i+m));<br />
&nbsp; &nbsp; &nbsp; &nbsp; str2<i>='\0';<br />
 &nbsp; &nbsp; &nbsp; &nbsp; puts(str2); <br />
}
Mack2
2011-04-14 16:24
4
#include &lt;stdio.h&gt;<br />
#include &lt;string.h&gt;<br />
void mcopy(char *s,int m)<br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; char str2[100];<br />
&nbsp; &nbsp; &nbsp; &nbsp; int i,n;<br />
&nbsp; &nbsp; &nbsp; &nbsp; n=strlen(s);<br />
&nbsp; &nbsp; &nbsp; &nbsp; for (i=0;i&lt;=n-m;i++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str2<i>=*(s+i+m-1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; str2<i>='\0';<br />
&nbsp; &nbsp; &nbsp; &nbsp; puts(str2);<br />
};<br />
int main(void )<br />
{<br />
 int m;<br />
 char str1[100];<br />
 puts(&quot;please input a string:&quot;);<br />
 gets(str1);<br />
 printf(&quot;please input m:&quot;);<br />
 scanf(&quot;%d&quot;,&amp;m);<br />
&nbsp; &nbsp; mcopy(str1,m);<br />
&nbsp; &nbsp; return 0;<br />
}
啊1624739559
2011-04-14 23:09
5
恭喜<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
你的文已解决
潜修僧
2011-04-20 14:17
6
for(i = 0; i &lt; n-m; i++)<br />
 str2<i>=str1[i+m];<br />
后面再加这个语句:str2<i>=0;<br />
还有就是既然你选择定义的函数返回什为void,那么main()中也就没有必要定义char <br />
str2[100]了。
shuo898
2011-05-06 01:55
7
基础还没有打好,回去了好好再看一下基础的东西!!给你的建议!
寂寞的灵魂
2011-05-22 15:17
8
#include &quot;stdio.h&quot;<br />
#include &quot;string.h&quot; <br />
<br />
char str1[100],str2[100];//定义全局变量;<br />
<br />
void mcopy(char *s , int m);<br />
int main(void )<br />
{<br />
 int m;<br />
 //char str1[100],str2[100];<br />
 puts(&quot;please input a string:&quot;);<br />
 gets(str1);<br />
 printf(&quot;please input m:&quot;);<br />
 scanf(&quot;%d&quot;,&amp;m);<br />
&nbsp; &nbsp; mcopy(str1,m);<br />
&nbsp; &nbsp; return 0;<br />
}<br />
void mcopy(char *s , int m)<br />
{<br />
// char str1[100],str2[100];<br />
 int i,n;<br />
 n=strlen(str1);<br />
 for(i = 0; i &lt; n-m; i++)<br />
 str2<i>=s[i+m];<br />
 puts(str2); <br />
}<br />
<br />
按照你的思路,应该只要把str1[100]和str2[100]定义成全局变量就可以了,程序就没什么大问题了,可以加str2<i>='\0'(本人不同意加str2='\0',常量地址怎么能赋值?),也可以不加(定义字符数组为全局变量时,编译器自动将其编译成空串),这是我写的void mcopy(char *s , int m):<br />
void mcopy(char *s , int m)<br />
{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;char*p=s;<br />
&nbsp; &nbsp; &nbsp; &nbsp; while(*s++!=p[m-1]);<br />
&nbsp; &nbsp; &nbsp; &nbsp; p=str2;<br />
&nbsp; &nbsp; &nbsp; &nbsp; while(*s&amp;&amp;(*p++=*s++));<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; *p='\0';//最好加上!!!<br />
&nbsp; &nbsp; &nbsp; &nbsp; puts(str2);<br />
}<br />
指针是C语言的灵魂,多用用。
寂寞的灵魂
2011-05-22 15:18
9
是str2<i>='\0';
寂寞的灵魂
2011-05-22 15:21
10
我晕,竟然显示不了!!郁闷!那改成*(str2+i);
寂寞的灵魂
2011-05-22 15:23
11
<img src="image/face/6.gif" class="face"><img src="image/face/7.gif" class="face">*(str2+i)='\0';<img src="image/face/21.gif" class="face"><img src="image/face/29.gif" class="face">
游客请输入验证码
文章分类
文章归档