问题 问答题

函数ReadDat()的功能是实现从文件IN5.DAT中读取一篇英文文章存入到字符串数组xx中。请编制函数ConvertCharA(),该函数的功能是:以行为单位把字符串中的所有小写字母改写成该字母的下一个字母,如果是字母z,则改写成字母a。大写字母仍为大写字母,小写字母仍为小写字母,其他字符不变。把已处理的字符串仍按行重新存入字符串数组xx中,最后调用函数WriteDat()把结果xx输出到文件OUT5.DAT中。例如,原文:Adb.Bcdzaabck.LLhj结果:Aec.Bdeabbcdl.LLik原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。注意:部分源程序已给出。请勿改动主函数main()、读函数ReadDat()和写函数WriteDat()的内容。试题程序:#include #include #include char xx[50][80];int maxline = 0;/* 文章的总行数 */int ReadDat(void);void WriteDat(void);void ConvertCharA(void){}main(){clrscr();if(ReadDat()){printf("数据文件IN5.DAT不能打开!\n\007");return;}ConvertCharA();WriteDat();}int ReadDat(void){FILE *fp;int i =0;char *p;if((fp = fopen("IN5.DAT", "r")) == NULL) return 1;while(fgets(xx[i], 80, fp) !=NULL){p = strchr(xx[i], ’’\n’’);if(p) *p = 0;i++;}maxline = i;fclose(fp);return 0;}void WriteDat(void){FILE *fp;int i;clrscr();fp = fopen("OUT5.DAT", "w");for(i = 0; i < maxline; i++) {printf("%s\n", xx[i]);fprintf(fp, "%s\n", xx[i]);}fclose(fp);}

答案

参考答案:

void ConvertCharA(void){int i,j,str;for(i=0;i=’a’ && xx[i][j]<=’y’) xx[i][j]+=A;   /*如果该字符是从a到y的小写字母则改写为该字母的下一个字母*/    }}

解析:

本题要对二维数组中的字符元素按行来处理,因此,首先要求得当前行所包含的字符总个数,之后才可以利用一个循环来依次访问该行中的所有字符。当遇到一个字符时,先判断该字符是否是小写字母z,如果是,则把该字符改写成小写字母a;如果不是,则继续判断该字符是否是从a到y的小写字母,如果是,则改写为该字母的下一个字母(利用字符的ASCII码值来实现);如果不是,则不做任何的操作,接着去取下一个字符。

单项选择题
单项选择题

A


Napoga is a 12-year-old girl in Ghana(加纳), Africa. It is hard for her family to get clean water. Every morning, she leaves home at half past five to get clean water for her family in a village far away. It takes her six hours to get enough clean water for daily (日常的) cooking and drinking. She has no time to go to school or to play with her friends. Millions of people in the world are like Napoga. They can’t get enough clean water to keep healthy.
Earth Day is April 22. But on all other days, we must also remember it. The water we use is the most important natural resource(自然资源) on the earth.
Water covers 70% of the earth’s surface(表面). But most of that is sea water. We can’t use it for very many things. Fresh water covers only 1% of the earth’s surface.
You probably feel lucky that your life isn’t as hard as Napoga’s. But that doesn’t mean you don’t have to worry about water. We all face serious water problems. One of them is water pollution. All kinds of things from cars, factories, farms and homes make our rivers, lakes, and oceans dirty. Polluted water is very bad for people to drink. And dirty water is bad for fish, too. Now, 34% of all kinds of fish are dying out.
How do cars and factories make our water dirty First, they pollute the air. Then, when it rains, the rain water comes down and makes our drinking water dirty. Dirty rain, called acid rain (酸雨), is also bad for plants, animals and buildings.
Scientists say that in 30 years, more than half of the people in the world won’t have enough clean water. We have to learn how to save more water for ourselves and our children. Here is some advice for saving water:
Turn off the water while you brush your teeth. You can save as much as 450 liters (升) each month.
Leaky taps(水的龙头) waste a lot of water. Fix them right away!
You can easily cut your 1O-minute showers(沐浴) in half—and you’ll be just as clean.
When you wash dishes, don’t let the water run.
Only wash clothes when you have a lot to wash. If your washing machine isn’t full, you’re wasting water!

Which of the following wastes water ( )

A.We need ten minutes if we want a clean shower.

B.We should put in enough clothes when we wash them in washing machines.

C.You can keep yourself clean by having a shower for five minutes.

D.Don’t let the water run when we brush our teeth and wash dishes.