c语言的问题!!!!!!!!!!!!!!!!!!!!!

来源:百度知道 编辑:UC知道 时间:2024/06/07 06:57:32
2. 改错题
下列给定程序中,函数fun()的功能是将字符串s中位于偶数位置的字符或ASCII码为奇数的字符放入字符串t中(规定第一个字符放在第0位中)。
例如:字符串中的数据为ADFESHDI,则输出应当是AFESDI。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <conio.h>
#include <stdio.h>
#include <string.h>
#define N 80
/**********************found***********************/

void fun(char s, char t[ ])
{
int i, j=0;
for(i=0; i<strlen(s);i++)
if(i%2==0||s[i]%2!=0)
t[j++]=s[i] ;
t[j]='\0';
}

main()
{
char s[N], t[N];
clrscr();
printf("\nPlease enter string s :");
gets(s);
fun(s,t);
printf("\nThe result is :%s\n",t);
}
found下面的char s 应该改成char *s 但我把它改成char s[]可以吗? 为什么?!

不可以,数组大小不确定了,而且数组是不可以整个传给函数的。就像字符串不可以直接赋值一样。函数要传出t的内容,要用地址。
我改的如下:改的行用*****标注。运行时去掉就可以了
#include <conio.h>
#include <stdio.h>
#include <string.h>
#define N 80
/**********************found***********************/

void fun(char *s, char *t) ********
{
int i, j=0;
for(i=0; i<strlen(s);i++)
if(i%2==0||s[i]%2!=0)
*(t+j++)=s[i] ; *******
*(t+j)='\0'; *********
}

main()
{
char s[N], t[N];
clrscr();
printf("\nPlease enter string s :");
gets(s);
fun(&s,&t); ********
printf("\nThe result is :%s\n",t);
}