Day6.5+周末

这些代码示例使用C语言实现了不同的功能:1)输出杨辉三角;2)打印字符形状,如字母A的放大版;3)判断并找出犯人D;4)检查输入数字是否能被预设数组中的数整除;5)反转字符串及替换空格;6)计算字符串中数字的总和;7)输出非空字符;8)分别使用循环计算字符串长度、复制、连接及比较字符串。
摘要由CSDN通过智能技术生成

 1.杨辉三角

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6 
  7     int a[6][6];
  8     for(int i=0;i<6;i++)
  9 {
 10     for(int j=0;j<=6-i;j++)
 11     {
 12     printf(" ");
 13     }
 14 
 15 
 16     for(int j=0;j<=i;j++)
 17     {
 18     if(i==j||j==0)
 19 
 20         {a[i][j]=1;}
 21     else
 22         {a[i][j]=a[i-1][j]+a[i-1][j-1];}
 23     
 24     printf("%2d",a[i][j]);
 25     }
 26     putchar(10);
 27 }
 28 
 29     return 0;                                                   
 30 }

2.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {   char n='A';
  6     for(int i=0;i<4;i++)
  7     {
  8     for(int j=0;j<=i;j++)
  9     {
 10     printf("%c",n);
 11     }
 12     printf("\n");
 13     n+=1;
 14     }
 15     putchar(10);
 16     n-=1;                                                       
 17     for(int i=0;i<4;i++)
 18     {
 19     for(int j=0;j<4-i;j++)
 20     {
 21     printf("%c",n);
 22     }
 23     printf("\n");
 24     n-=1;
 25     }
 26     return 0;
 27 }

3.

 犯人是D

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     char k;
  7     for(k='a';k<='d';k++)
  8     {
  9     if((k!='a')+(k=='c')+(k=='a'||k=='d')+(k=='c')==2)
 10 
 11     {break;}
 12         
 13     }
 14     printf("k=%c\n",k);
 15     return 0;
 16 }

4.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {   printf("请输入一个正整数a:\n");
  6     int a;
  7     scanf("%d",&a);
  8     int b[3]={3,5,7};
  9     int count=0;
 10     for(int i=0;i<3;i++)
 11     {
 12     if(a%b[i]==0)
 13     {
 14     printf("%d ",b[i]);
 15     count++;
 16     }
 17 
 18     }
 19     if(count==0)
 20         printf("n");
 21     putchar(10);
 22 
 23     return 0;
 24 }    

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[]="good    good    study";
  7     int i=0,k;
  8     int j=strlen(a)-1;
  9     while(i<j)
 10     {
 11     char t=a[i];a[i]=a[j];a[j]=t;
 12     i++;j--;
 13     }
 14     i=0;j=0;
 15     while(a[i]!='\0')
 16 {
 17     while(a[j]!=' '&&a[j]!='\0')
 18     {j++;}
 19     k=j-1;
 20 
 21     while(i<k)
 22     {
 23     char t=a[i];a[i]=a[k];a[k]=t;
 24     i++;k--;
 25     }
 26 
 27     while(a[j]==' ')
 28    {    j++;}
 29     i=j;                                                        
 30 
 31 
 32 
 33 }
 34 puts(a);
 35 
 36     return 0;
 37 }

6.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     char a[]={"45628"};
  7     int sum=0;
  8     for(int i=0;i<strlen(a);i++)
  9     {
 10     sum=sum*10+a[i]-'0';
 11     }
 12     printf("sum=%d\n",sum);
 13     return 0;                                                   
 14 }

 7.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     char a[]="this  is  dog";
  7     for(int i=0;i<strlen(a);i++)
  8     {
  9     if(a[i]!=' ')
 10         printf("%c",a[i]);
 11     }
 12     putchar(10);
 13     return 0;
 14 }

8.

(1)strlen:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6     char a[]="hello boy";
  7     int count=0;
  8     for(int i=0;a[i]!='\0';i++)
  9     {
 10     count++;
 11     }
 12     printf("字符串的长度是:%d\n",count);
 13     return 0;
 14 }

 (2)strcpy

 1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 int main(int argc, const char *argv[])
  5 {
  6 /*  char a[]="hello boy";
  7     int count=0;
  8     for(int i=0;a[i]!='\0';i++)
  9     {
 10     count++;
 11     }
 12     printf("字符串的长度是:%d\n",count);
 13 */
 14     char a[]="hello";
 15     char b[]="ABC";
 16     int i;
 17     for(i=0;b[i]!='\0';i++)
 18     {
 19     a[i]=b[i];
 20     }
 21     a[i]=b[i];
 22     puts(a);
 23     puts(b);
 24     return 0;
 25 }

 (3)strcat

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    char a[20]="vrey ";
    char b[]="good!";                                               
    int i;
    for(i=0;a[i]!='\0';i++)
    {   
    }   
    for(int j=0;b[j]!=0;j++)
    {   
    a[i]=b[j];
    i++;
    }   
    a[i]='\0';
    puts(a);puts(b);
    return 0;
}

(4)strcmp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    char a[]="hello";
    char b[]="helloO";
    int i;
    for(i=0;a[i]==b[i];i++)                                         
    {
    if(a[i]=='\0')
        break;
    }
    if(a[i]>a[i])
    {
    printf("a>b");
    }
    else if(a[i]<b[i])
    {
    printf("a<b");
    }
    else
    {printf("a=b");}
    int t=a[i]-b[i];
    printf("%d\n",t);
    return 0;
}

xmind

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值