请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
#define M 5
#define N 20
void fun(char (*ss)[N])
{int i, j, n, len=0;
for(i=0; i
{len=strlen(ss[i]);
if(i==0) n=len;
if(len>n)n=len;
}
for(i=0; i
/**********found**********/
n=strlen(___1___);
for(j=0; j
/**********found**********/
ss[i][___2___]='*';
/**********found**********/
ss[i][n+j+ ___3___]='\0';
}
}
main()
{char ss[M][N]={"shanghai","guangzhou","beijing", "tianjing","cchongqing"};
int i;
printf("The original strings are :\n");
for(i=0; i
printf("\n");
fun(ss);
printf("The result is :\n");
for(i=0; i
}
解题答案:
/**********第一空**********/
n=strlen(ss[i]);
/**********第二空**********/
ss[i][n+j]='*';
/**********第三空**********/
ss[i][n+j+ 1]='\0';
****************************************** [NextPage] 二、改错题:给定程序MODI1.C中 fun 函数的功能是:将p所指字符串中每个单词的最后一个字母改成大写。(这里的“单词”是指由空格隔开的字符串)。
例如, 若输入"I am a student to take the examination.", 则应输出 "I aM A studenT tO takE thE examination."。 请修改程序中的错误之处, 使它能得出正确的结果。
注意: 不要改动 main 函数, 不得删行, 也不得更改程序的结构!
给定源程序:
#include
#include
#include
void fun(char *p)
{
int k = 0;
for(; *p; p++)
if(k)
{
/**********found***********/
if(p == ' ')
{
k = 0;
/**********found***********/
* (p) = toupper(*(p - 1))
}
}
else
k = 1;
}
main()
{
char chrstr[64];
int d ;
printf("\nPlease enter an English sentence within 63 letters: ");
gets(chrstr);
d=strlen(chrstr);
chrstr[d] = ' ' ;
chrstr[d+1] = 0 ;
printf("\n\nBefore changing:\n %s", chrstr);
fun(chrstr);
printf("\nAfter changing:\n %s", chrstr);
}
解题答案:
/**********found***********/
if(*p==' ')
/**********found***********/
*(p-1)=toupper(*(p-1));
******************************************
[NextPage] 三、程序题:请编写函数fun, 对长度为7个字符的字符串, 除首、尾字符外,将其余5个字符按ASCII码降序排列。例如,原来的字符串为 CEAedca,排序后输出为 CedcEAa 。注意: 部分源程序在文件PROG1.C中。请勿改动主函数main和其它函数中的任何内容, 仅在函数fun的花括号中填入
你编写的若干语句。
给定源程序:
#include
#include
#include
void fun(char *s,int num)
{
}
NONO()
{
/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
char s[10] ;
FILE *rf, *wf ;
int i = 0 ;
rf = fopen("in.dat","r");
wf = fopen("out.dat","w");
while(i < 10) {
fgets(s,10,rf);
s[7] = 0 ;
fun(s,7);
fprintf(wf, "%s\n", s);
i++ ;
}
fclose(rf);
fclose(wf);
}
main()
{
char s[10];
printf("输入7个字符的字符串:");
gets(s);
fun(s,7);
printf("\n%s",s);
NONO();
}
参考答案:
int fun(char *s,int num)
{
char ch ;
int i, j ;
for(i = 1 ; i < 6 ; i++)
for(j = i + 1 ; j < 6 ; j++) {
if(*(s + i) < *(s + j)) {
ch = *(s + j) ;
*(s + j) = *(s +i) ;
*(s + i) = ch ;
}
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|