4.8作业

1.循环输入5个人姓名(二维字符数组),计算最大值

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     char a[5][100];
  7     int i;
  8     for(i=0;i<5;i++)
  9     scanf("%s",a[i]);                      
 10     char max[100];
 11     strcpy(max,a[0]);
 12     for(i=0;i<5;i++)
 13     {
 14         if(strcmp(max,a[i])<0)
 15         strcpy(max,a[i]);
 16     }
 17     printf("最大值为%s\n",max);
 18     return 0;
 19 }

2.在主函数中定义字符串数组并初始化,自定有参有返函数,计算字符串长度,并返回长度

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 unsigned long length(char str[5])
  5 {
  6     int i=0;
  7     for(i=0;str[i]!='0';i++);
  8     return i;
  9 }
 10 int main(int argc, const char *argv[])
 11 {
 12     char str[5]="abcd";
 13     unsigned long len=length(str);
 14     printf("%ld\n",len);
 15     return 0;
 16 } 

3.在主函数中定义两个字符串并初始化,定义有参无返函数,实现字符串连接

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 void fun(char a[20],char b[20])
  5 {
  6     int i,j;
  7     for(i=0;a[i]!='\0';i++);
  8     for(j=0;b[j]!='\0';j++)
  9         a[i++]=b[j];
 10     a[i]='\0';
 11     puts(a);                              
 12 }
 13 int main(int argc, const char *argv[])
 14 {
 15     char a[20]="hello";
 16     char b[20]="world";
 17     fun(a,b);
 18     return 0;
 19 }

4.在主函数中定义两个字符串并初始化,定义有参有返函数,实现字符串比较,返回两个字符串ASCII值的差

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int ASCII(char s1[],char s2[])
  5 {
  6     int i=0;
  7     while(s1[i]==s2[i])
  8     {
  9         if(s1[i]=='\0')
 10             break;
 11         i++;
 12     }
 13     int sub=s1[i]-s2[i];
 14     if(sub>0) puts("s1>s2");
 15     else if(sub<0) puts("s1<s2");
 16     else puts("s1==s2");
 17     return sub;
 18 }
 19 int main(int argc, const char *argv[])
 20 {
 21     char s1[]="ABCD";
 22     char s2[]="ABCd";
 23     ASCII(s1,s2);
 24     printf("%d\n",ASCII(s1,s2));
 25     return 0;
 26 }

思维导图:

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值