鱼C论坛

 找回密码
 立即注册
查看: 2838|回复: 1

假设某班级所有学生的姓名保存在一个字符串数组中,设计一个函数完成以下功能。

[复制链接]
发表于 2014-6-11 15:02:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
函数原型如下:
int lookup(char *src[],int n,char *tag[]);
其中,src是学生名数组的首地址,n是学生人数。要求找出所有姓刘的同学,在数组tag中保存他们的名字,返回刘姓学生的数量。

这是作业题 实在想不出来 求大侠帮忙!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-6-11 15:29:25 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define NUM 100

  4. int lookup(char *src[],int n,char *tag[]);               
  5.                 //src学生名数组首地址 n学生人数 tag保存刘姓学生

  6. int main()
  7. {
  8.         char *src[NUM],*tag[NUM];
  9.         int n,i,count=0;                        //n输入人数 i行下标 count计算刘个数

  10.         printf("\n\n\n\n\n\t\t\t\t请输入学生个数  ");
  11.         scanf("%d",&n);
  12.         rewind(stdin);                //清除回车
  13.        
  14.         for(i=0;i<n;i++)
  15.         {
  16.                 gets(src[i]);
  17.         }
  18.        
  19.         count=lookup(src,n,tag);
  20.         printf("\n\t\t\t\t一共有刘氏学生%d个",count);

  21.         for(i=0;i<count;i++)
  22.         {
  23.                 puts(src[i]);
  24.         }
  25.        
  26.         return 0;
  27. }

  28. int lookup(char *src[],int n,char *tag[])
  29. {
  30.         char liu[] = "刘";
  31.         int count = 0,i;

  32.         for(i=0;i<n;i++)
  33.         {
  34.                 if(!strcmp(src[i],liu))
  35.                 {
  36.                         strcpy(tag[count],src[i]);
  37.                         count ++;
  38.                 }
  39.         }
  40.         return count;
  41. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 04:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表