全国计算机等级考试二级c机试试题6_16(46)(字符串处理专场2).docVIP

  • 14
  • 0
  • 约2.36千字
  • 约 3页
  • 2021-01-26 发布于河北
  • 举报

全国计算机等级考试二级c机试试题6_16(46)(字符串处理专场2).doc

全国计算机等级考试二级C机试试题6_16 填空题 下列给定程序中,函数fun的功能是:将形参s所指字符串中的所有字母字符顺序前移,其他字符顺序后移,处理后将新字符串的首地址作为函数值返回。 例如,若s所指字符串为:asd123fgh543df,处理后新字符串为:asdfghdf123543。 请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。 注意:部分源程序在文件BLANK1.C中。 不得增行或删行,也不得更改程序的结构! #include stdio.h #include stdlib.h #include string.h char *fun(char *s) { int i, j, k, n; char *p, *t; n=strlen(s)+1; t=(char*)malloc(n*sizeof(char)); p=(char*)malloc(n*sizeof(char)); j=0; k=0; for(i=0; in; i++) { if(((s[i]=a)(s[i]=z))||((s[i]=A)(s[i]=Z))) { /**********found**********/ t[j]=__1__; j++;} else { p[k]=s[i]; k++; } } /**********found**********/ for(i=0; i__2__; i++) t[j+i]=p[i]; /**********found**********/ t[j+k]= __3__; return t; } main() { char s[80]; printf(Please input: ); scanf(%s,s); printf(\nThe result is: %s\n,fun(s)); } 改错题 下列给定程序中函数fun的功能是:将s所指字符串中最后一次出现的与t1所指字符串相同的子串替换成t2所指字符串,所形成的新串放在w所指的数组中。要求t1和t2所指字符串的长度相同。 例如,当s所指字符串中的内容为:abcdabfabc,t1所指串中的内容为ab,t2所指子串中的内容为99时,结果在w所指的数组中的内容应为abcdabf99c。 请改正程序中的错误,使它能得出正确的结果。 注意:部分源程序在文件MODI1.C中,不得增行或删行,也不得更改的程序的结构! #include stdio.h #include string.h void fun (char *s, char *t1, char *t2 , char *w) { char *p , *r, *a=s; strcpy( w, s ); /************found************/ while ( w ) { p = w; r = t1; while ( *r ) /************found************/ IF ( *r == *p ) { r++; p++; } else break; if ( *r == \0 ) a = w; w++; } r = t2; while ( *r ){ *a = *r; a++; r++; } } main() { char s[100], t1[100], t2[100], w[100]; printf(\nPlease enter string S:); scanf(%s, s); printf(\nPlease enter substring t1:); scanf(%s, t1); printf(\nPlease enter substring t2:); scanf(%s, t2); if ( strlen(t1)==strlen(t2) ) { fun( s, t1, t2, w); printf(\nThe result is : %s\n, w); } else printf(\nError : strlen(t1) != strlen(t2)\n); } 编程题 编写函数fun,其功能是:将s所指字符串中ASCII值为奇数的字符删除,剩余字符形成的新串放在t所指数组中。 例如,若s所指字符串中的内容为:ABCDEFG12345,其中字符A的ASCII码值为奇数、…、字符1的ASCII码值也为奇数、…都应当删除,其它依此类推。最后t所指的数组中

文档评论(0)

1亿VIP精品文档

相关文档