2.14:二维数组、非函数实现strcat、strcmp、strcpy、strlen

1.编程实现二维数组的杨辉三角

程序代码:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n;
  7     printf("please enter n:");
  8     scanf("%d",&n);
  9     int arr[n][n];
 10     for(int i=0;i<n;i++)
 11     {
 12         for(int j=0;j<=i;j++)
 13         {
 14             if(j==0||j==i)
 15                 arr[i][j]=1;
 16             else
 17                 arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
 18             printf("%-4d",arr[i][j]);
 19         }
 20         puts("");
 21     }
 22     return 0;
 23 } 

运行结果:

 2.编程实现二维数组计算每一行的和以及列和

程序代码:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int n,m;
  7     printf("please enter n:");
  8     scanf("%d",&n);
  9     printf("please enter m:");
 10     scanf("%d",&m);
 11     int arr[n][m];
 12     for(int i=0;i<n;i++)
 13     {
 14         for(int j=0;j<m;j++)
 15         {
 16             scanf("%d",&arr[i][j]);
 17         }
 18     }
 19     //行和
 20     for(int i=0;i<n;i++)
 21     {
 22         int row_sum=0;
 23         for(int j=0;j<m;j++)
 24             row_sum+=arr[i][j];
 25         printf("%d row_sum=%d\n",i+1,row_sum);
 26     }
 27     //列和
 28     for(int j=0;j<m;j++)
 29     {
 30         int line_sum=0;
 31         for(int i=0;i<n;i++)
 32             line_sum+=arr[i][j];
 33         printf("%d line_sum=%d\n",j+1,line_sum);
 34     }
 35     return 0;
 36 }   

运行结果:

 3.编程实现二维数组计算第二大值

程序代码:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     int arr[2][3];
  7     for(int i=0;i<2;i++)
  8     {
  9         for(int j=0;j<3;j++)
 10             scanf("%d",&arr[i][j]);
 11     }
 12     int max=arr[0][0];
 13     int second_max=arr[0][0];
 14     for(int i=0;i<2;i++)
 15     {
 16         for(int j=0;j<3;j++)
 17         {
 18             if(max<arr[i][j])
 19                 max=arr[i][j];
 20             if(second_max>arr[i][j])
 21                 second_max=arr[i][j];
 22         }
 23     }
 24     for(int i=0;i<2;i++)
 25     {
 26         for(int j=0;j<3;j++)
 27         {
 28             if(max==arr[i][j])
 29                 continue;
 30             if(second_max<arr[i][j])
 31                 second_max=arr[i][j];
 32         }
 33     }
 34     printf("second_max=%d\n",second_max);
 35     return 0;
 36 }  

运行结果:

 4.非函数实现系统函数strcat、strcmp、strcpy、strlen

程序代码:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 void my_strcat(char *dest,char *src);
  5 void my_strcmp(char *dest,char *src);
  6 void my_strcpy(char *dest,char *src);
  7 int my_strlen(char *dest);
  8 int main(int argc, const char *argv[])
  9 {
 10     char dest[20]="";
 11     char src[20]="";
 12     printf("please enter dest:");
 13     gets(dest);
 14     printf("please enter src:");
 15     gets(src);
 16 
 17     my_strcat(dest,src);
 18     puts(dest);
 19 
 20     my_strcmp(dest,src);
 21 
 22     my_strcpy(dest,src);
 23     puts(dest);
 24 
 25     int len=my_strlen(dest);
 26     printf("strlen=%d\n",len);
 27     return 0;
 28 }
 29 void my_strcat(char *dest,char *src)
 30 {
 31     int i,j;
 32     for(i=0;*(dest+i)!='\0';i++);
 33     for(j=0;*(src+j)!='\0';j++)
 34         *(dest+i+j)=*(src+j);
 35     *(dest+i+j)='\0';
 36 }
 37 void my_strcmp(char *dest,char *src)
 38 {
 39     int i=0;
 40     while(dest[i]==src[i])
 41     {
 42         if(dest[i]=='\0')
 43             break;
 44         i++;
 45     }
 46     if(dest[i]-src[i]>0)
 47         puts("dest>src");
 48     else if(dest[i]-src[i]<0)
 49         puts("dest<src");
 50     else if(dest[i]-src[i]==0)
 51         puts("dest==src");
 52 }
 53 void my_strcpy(char *dest,char *src)
 54 {
 55     int i;
 56     for(i=0;*(src+i)!='\0';i++)
 57         *(dest+i)=*(src+i);
 58     *(dest+i)='\0';
 59 }
 60 int my_strlen(char *dest)
 61 {
 62     int i;
 63     for(i=0;*(dest+i)!='\0';i++);
 64     return i;
 65 } 

运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值