问题 问答题

规定输入的字符串中只包含字母和*号。请编写函数fun,其功能是:使字符串中尾部的*号不多于n个,若多于n个,则删除多余的*号;若少于或等于n个,则不做任何操作,字符串中间和前面的*号不删除。
例如,字符串中的内容为“****A*BC*DEF*G*******”,若n的值为4,删除后,字符串中的内容应为“****A*BC*DEF*G****”;若n的值为7,则字符串中的内容仍为“****A*BC*DEF*G*******”。n的值在主函数中输入。编写函数时,不得使用C语言提供的字符串函数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
试题程序:
#include<stdio.h>
void fun(char*a,int n)


main()

char s[81];int n;
printf("Enter a string:\n");
gets(s);
printf("Enter n:");
scanf("%d",&n);
fun(s,n);
printf("The string after deleted:\n");
puts(s);

答案

参考答案:

 void fun(char*a,int n)

{

int i=0,k=0;

char*p,*t;

p=t=a; /*将指针移动到字符串末尾*/

while(*t)

t++;

t--; /*从后往前如果是’*’ 则使k++,找到最后一个*所在的位置,并记录’*’的个数*/

while(*t==’*’)/*指针t指向前一个,同时标量k增加一*/

{k++;t--;}

if(k>n)

{while(*p&&p<t+n+A)

{ a[i]=*p;

i++;p++;

}

a[i]=’\0’;

}

}

解析:

字符串中尾部*号不能多于n个,多余的要删除。首先需要通过while循环统计字符串尾部*号,然后通过if条件语句完成尾部*号数和n的比较,若尾部*号数多于n个,则需要把n个*号和其余字符重新保留。

单项选择题
阅读理解

第三部分:阅读理解(共20小题;每小题2分,满分40分)

阅读下列短文,从每题所给的四个选项(A、B、C、D)中,选出最佳选项。

A man accused of failing to return more than 700 children's books to five different libraries in the county was released from prison after a book publisher agreed to post his bond(保释金)of $1,000. The publisher said, "There's a story here. This is a man who loves books. He just can't let go of them. He hasn't stolen a single book. So what's the crime? We think that Mr Banish has a story to tell. We plan to publish his story."

When asked why he didn't return the books, Mr Banish said, "Well, how could I? They became family to me. I was afraid to return them, because I knew that kids or dogs would get hold of these books and chew them up, throw them around, tear the pages, spill soda on them, get jam and jelly on them, and drown them in the toilet.

He continued, "Books are people, too! They talk to you, they take care of you, and they enrich you with wisdom, humor and love. A book is a guest in my home. How could I kick it out? I repaired torn pages. I dusted them with a soft clean cloth. I turned their pages so they could breathe and get some fresh air."

"Every week I reorganized them on their shelves so they could meet new friends. My books were HAPPY books. You could tell just by looking at them. Now they're all back in the libraries, on the lower shelves, on the floors, at the mercy of all those runny-nosed kids. I can hear them calling me. I need to rescue them. Excuse me. I have to go now."

56. Why was the man put into prison?

A. Because the book publisher persuaded the police to do so.

B. Because he stole 700 children's books from the five different libraries.

C. Because he refused to return the books that he had borrowed.

D. Because he wanted to publish his story.

57. How did the man treat books?

A. He treated them as real people.

B. He treated them as his own children.

C. He treated them as his furniture.

D. He treated them as his job.

58. From the passage we can learn that the man is _______.

A. a thief      B. a writer       C. crazy about books   D. unfortunate

59. What might happen after the man was set free?

A. He might stop borrowing books.

B. He might start a library of his own.

C. He might go on borrowing books from libraries.