填空题
1. 下列给定程序中,函数fun的功能是:依次取出字符串中所有的数字字符,形成新的字符串,并取代原字符串。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void fun(char *s)
{
int i,j;
for(i=0,j=0;s[i]!='\0';i++)
if(s[i]>='0'&&s[i]<='9')
/*********found*********/
s[j]=s[i];
/*********found*********/
s[j]="\0";
}
void main()
{
char item[80];
system("CLS");
printf("\nEnter a string:");
gets(item);
prLntf ("\n\nThe string is:%s\n",item);
fun(item);
printf ("\n\nThe string of changing is:%s\n",item);
}
【正确答案】
1、(1)s[j++]=s[i];
(2)s[j]='\0';
【答案解析】
题目要求依次取出字符串中所有的数字字符,因此将元素s[i]存入s[j]后要使j加1,为下次存储做准备。s[j]="\0";是一个语法错误。