简答题 C语言
题号 #2257003

下列给定程序中,函数proc()的功能是:依次取出字符串中所有的数字字符,形成新的字符串,并取代原字符串。例如原始字符串是“ab45c6djfd789”,则输出后的字符是
“456789”。
请修改函数proc()中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>-
void proc(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 str[80];
system("CLS"):
printf("\nEnter a string:");gets(str);
printf("\n\nThe string is:%s\n",st r);
proc(str);
printf("{\n\nThe string of changing is:
%s\n",str);
}

进入在线模考

参考答案

暂无

答案解析

(1)错误:s[i]=s[i]
正确:s[j++]=s[i]
(2)错误:s[j]="\0";
正确:s[j]='\0';
【解析】题目要求将字符串q-的数字字符形成新字符串并取代原字符串,每检查到一个数字字符,将其放在原字符串下标为0处开始存放,下标加1,因此“s[j]=s[i]”应改为“s[j++]=s[i]”。要使数字字符串成为独立的字符串,应该在数字字符串末尾加上结束符,字符串的结束符为'\0 ',因此“s[j]="\0"”应改为“s[j]='\0'”。

你可能感兴趣的试题

1

学生的记录由学号和成绩组成,M名学生的数据已在主函数中放入结构体数组stu中,请编写函数[)roc(),它的功能是:把指定分数范围内的学生数据放在b所指的数组中,分数范围内的学生人数由函数值返回。
例如,输入的分数是70~79,则应当把分数在70~79 的学生数据进行输出,包含70分和79分的学生数据。主函数中把70放在low中,把79放在heigh中。
注意:部分源程序给出如下。
清勿改动main()函数和其他函数中的任何内容,仅在函数proc的花括号中填入所编写的若干语句。
试题程序:
#include<stdio.h>
#define M 16
typedef struet
{  char num[10];
int s;
}
STREC:
int proc(STREC*a,STREC*b,int 1,int h)
{

}

void main()
{
STREC stu[M]={{"GA005",85},{"GA003",
76},
{"GA002",69},{"GA004",85),{"GA001",96),
{"GA007",72),{"GA008",64),{"GA006",87},
{"GA015",85},{"GA013",94),{"GA012",64),
{"GA014",91},{"GA011",9O),{"GA017",64),
{"GA018",64),{"GAOl6",72));
STREC h[M];
int i,n,low,heigh,t;
printf("Enter 2 integer number low&
heigh:");
scanf("%d%d",&low,&heigh);
if(heigh<low)
{t=heigh;heigh=low;low=t;)
n=proc(stu,h,loW,heigh):
printf("The student’S data between
%d--%d:\n",low,heigh);
for(i=0;i<n;i++)
printf("%s%4d\n",hi-i].num,h[i].s);
//输出指定分数范围内的学生记录
printf("\n"):
}

找试题 · 找答案 · 找资料 在线模考