下列给定程序中,函数fun的功能是:将字符串s中的位于奇数位置的字符或ASCII码为偶数的字符依次放入字符串t中。例如,字符串中的数据为AABBCCDDEEFF,则输出应当是ABBC DDEFF。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
#define N 80
void fun (char *s,char t[ ])
{
int i,j = 0;
/********found********/
for (i=0; i<(int)strlen(s); i++)
if (i%2 && s[i]%2==0)
t[j++] = s[i];
/********found********/
t[i] = '\0';
}
main ( )
{
char s[N] ,t[N];
printf("\nPlease enther string s:");
gets(s);
fun(s,t);
printf("\nThe result is:%s\n",t);
}
(1)错误:if (i%2 && s[i]%2 ==0) 正确:if(i%2 || s[i]%2==0) 或if(i%2 !=0 ||
s[i]%2==0)
(2)错误:t[i] ='\0' 正确:t[j]='\0'; 或t[j]=0;