免费智能真题库 > 历年试卷 > 程序员 > 2018年上半年 程序员 下午试卷 案例
  第4题      
  知识点:   Word

 
【说明】
下面的C代码在输入的100个英文单词中找出最小单词和最大单词。约定每个单词是仅由英文字母构成的字符串,且都不超过20个字符。单词的大小按照字典序定义。例如,单词“entry”大于“enter”、“art”小于“ article”、“an”等于“An”。
 【C代码】
#include <stdio.h>
#define NUMBER 100
int isValid(const char *s1);                  //若字符串s1仅包含英文字母则返回1,否则返回0
char toLower(char ch);                       //将大写字母转换为小写字母
int usr_strcmp(char *s1, char *s2);    //比较字符串s1和s2,相等时返回0,
                                                            //s1大则返回正整数,s1小则返回负整数
void usr_strcpy(char *s1,const char *s2);     //字符串s2拷贝给s1

int main()
{       char word[32];
        char maxWord[32]="", minWord[32] ="";
        int numWord=0;
        while(num Word<NUMBER) {
             scanf("%s",    (1)    );                                 //输入一个单词存入word
             if(is Valid(word))     {
                   if (0==num Word) {usr_strcpy(min Word,word);usr_strcpy(max Word,word);} 
                   num Word++;
                   if(         (2)       >0)                              //调用usr_strcmp比较单词
                              usr_strcpy(max Word, word);      //用max Word记下最大单词
                   else
                              if(       (3)      <0)                     //调用usr_strcmp比较单词
                                    usr_strcpy(min Word,word); //用min Word记下最小单词
            }
      }
      printf("max Word=%s                min Word=%s\n",max Word,min Word);
      return 0;
}
int is Valid(const char *s)
{
     for(; *s ; s++)
         if(!(*s>='a' && *s<='z') && !(*s>='A' && *s<='Z'))
             return 0;
     return 1;
 }
 
char toLower(char ch)
{     //若ch为大写字母则返回其小写形式,否则直接返回原字符
      if(ch>='A' && ch<='Z')
                 ch=         (4)         +'a';
      return ch;
}
 
int usr_strcmp(char *s1,char *s2)
{    //按字典序比较两个英文单词,若s1表示的单词大,则返回正整数,
     //若s1表示的单词小,则返回负整数;否则返回0

     for(;    (5)    ;) {
              if(toLower(*s1)==toLower(*s2))    {s1++,s2++;}
              else
                    break;
      }
     return(toLower(*s1) - toLower(*s2));
}

void usr_strcpy(char *s1,const char *s2)
{    //将s2表示的字符串复制给s1
       for(;       (6)       ;)
            *s1++= *s2++;
       *s1='\0';
}
 
问题:4.1   (共15分)
阅读以下说明和C代码,填写代码中的空(1)~(6),将解答写入答题纸的对应栏内。
 
 
 

   知识点讲解    
   · Word
 
       Word
        Word是微软公司开发的最流行的文字处理程序。Word提供了许多易于使用的文档创建工具,同时也提供了丰富的功能集供创建复杂文档使用。
   题号导航      2018年上半年 程序员 下午试卷 案例   本试卷我的完整做题情况  
1 /
2 /
3 /
4 /
5 /
6 /
 
第4题    在手机中做本题