问答题

请编写函数fun(),其功能是:将s所指字符串中除了下标为奇数、同时ASCII值为偶数的字符外,其余的全部删除,串中剩余字符所形成的一个新串放在t所指的数组中。 例如,若s所指字符串中的内容为edB2A4Dsdg,其中字符A的ASCII码值为奇数,因此应当删除;其中字符B的ASCII码值为偶数,但在数组中的下标为偶数,因此也应当删除:而字符2的ASCII码值为偶数,所在数组中的下标为奇数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是d24。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 试题程序: #include <conio.h> #include <stdio.h> #include<string, h> void fun(char*s, char t[]) { } main() { char s[100] ,t[100]; clrscr(); printf("\nPlease enter string S: "); scanf("%s",s); fun(s,t); printf("\nThe result is: %s\n",t); }

【参考答案】

void fun(char*s, char t[]) { int i,j=0; for(i=0;s[i]!=’\0’......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

填空题
字符串str由数字字符组成(长度不超过5个字符),可看作任意进制的数,请补充函数fun(),该函数的功能是:把 str字符串转换成任意进制的数,结果保存在数组xx中,由函数返回转换后数组腆的实际长度。其中x表示str原来的进制, y表示要转换成的进制。例如,输入str=“1111”,x=2,y=10,结果输出:15。如果输入str=“15”, x=10,Y=2,结果输出: 1111。注意:部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。试题程序:#include <stdio.h>#include<stdlib.h>#include<string.h>#define N 8int xx[N];int fun(char *str,int x,int y){int sum;int i=0;char *p=str;for(i=0; i<N; i++)xx[i]=0;sum=*p-’0’;p++;while (*p){sum= 【1】 ;p++;}i=0;while(sum!=0){xx[i]= 【2】 ;【3】 ;i++;}return i;}main (){char str[6];int i;int n;int x;int y;printf( Enter a string made up of ’0’ to’9’ digits character: );gets(str);if(strlen (str) >5){printf( Error:string too longer!,please input again! n n );exit(0);}for(i=0;str[i];i++)if(str[i]<’0’||str[i]>’9’){printf( Error:%c not is ’0’ to’9’ digits character! n n ,str[i]);exit(0);}printf( The original string: );puts(str);printf( nINPUT x= );scanf( %d ,&x);printf( nINPUT y= );scanf( %d ,&y);n=fun(str,x,y);printf( n%s is convered to ,str);for (i=n-1; i>=0; i--)printf( %d ,xx[i]);}