第五次作业

文章展示了四个C语言编程示例:统计字符串中不同字符类型(大写、小写、数字等)、字母顺序翻转(IloveChina),以及生成杨三角数组。这些代码展示了基础的字符串处理和数组操作技巧。
摘要由CSDN通过智能技术生成

第一个:统计字符个数

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, const char *argv[])
{
    char str[128];
    gets(str);
    int coun=0;
    int ccc=0;
    int ppp=0;
    int qit=0;
    for(int i=0;i<strlen(str);i++)
    {
        printf("%d\n",i);
        if(str[i]<='Z' && str[i]>='A')
        {
            coun++;

        }
        else if(str[i]>='a' && str[i]<='z')
        {
            ccc++;


        }
        else if(isdigit(str[i]))
        {
            ppp++;
        }                                                                                                         
        else
        {
            qit++;

        }
    }
    printf("大写母有:%d 小写字母有:%d 数字有:%d 其他字符有%d",coun,ccc,ppp,qit);
    return 0;
}
                                                                                                                  

运行效果:

ubuntu@ubuntu:c_1$ ./a.out 
qweFFkk  ppmm]] nn 555;
大写母有:2 小写字母有:11 数字有:3 其他字符有7ubuntu@ubuntu:c_1$ ./a.out 
KKkkppPP  uu  yyyyv-0=0=0=;; 888
大写母有:4 小写字母有:11 数字有:6 其他字符有11ubuntu@ubuntu:c_1$ 

第二个:统计小写字符

 #include <stdio.h>
 #include <string.h>
 int main(int argc, const char *argv[])
 {                                                  
     char jj[]="aabbcchaaaah";
     int  hh[26]={0};
     for(int i=0;i<strlen(jj);i++ )
     {
 
         if(jj[i]>='a'&&jj[i]<='z')
         {
             hh[jj[i]-97]++;
         }
 
 
 
     }
     printf("a=%d",hh[0]);
     return 0;
 }
                                                    

运行效果:

ubuntu@ubuntu:c_1$ gcc 08.c
ubuntu@ubuntu:c_1$ ./a.out 
a=6ubuntu@ubuntu:c_1$ 

第三个:I love china

#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{

    char hh[]="I love China";
    printf("%s\n",hh);
    int i=0;
    int gg=7;
    char jj=' ';
    int k=7;
    for(int qq=7;qq<=11;qq++)
    {
        k=qq;
        while(1)
        {
            if(k!=i)
            {
                jj=hh[k];
                hh[k]=hh[k-1];
                hh[k-1]=jj;
            }
            else
            {
                break;
            }
            --k;
        }
        ++i;
    }

    int III=5;
    char jjjj=' ';
    jjjj=hh[III];
    hh[III]=hh[III+1];
    hh[III+1]=jjjj;
    char ptp=' ';
    for(int uu=6;uu<11;uu++)
    {
        ptp=hh[uu];
        hh[uu]=hh[uu+1];
        hh[uu+1]=ptp;

    }
    printf("%s\n",hh);
    return 0;
}                                                                                                    

运行结果:

ubuntu@ubuntu:c_1$ ./a.out 
I love China
China love I
ubuntu@ubuntu:c_1$ 

第四个:杨三角

 #include <stdio.h>
 int main(int argc, const char *argv[])
 {
     int arr[10][10]={0};
     for(int i=0;i<10;i++)
     {
         arr[i][0]=1;
 
     }
     for(int u=1;u<10;u++)
     {
         for(int r=1;r<10;r++)
         {
             arr[u][r]=arr[u-1][r]+arr[u-1][r-1];
 
         }
 
     }
     for(int yy=0;yy<10;yy++)
     {
         for(int ww=0;ww<=yy;ww++)
         {
             printf("%d\t",arr[yy][ww]);
         }
         printf("\n");
     }
 
 
     return 0;
 }                                                                                   
                                                                                     
                                                                                     

运行结果:

ubuntu@ubuntu:c_1$ gcc 06.c
ubuntu@ubuntu:c_1$ ./a.out 
1	
1	1	
1	2	1	
1	3	3	1	
1	4	6	4	1	
1	5	10	10	5	1	
1	6	15	20	15	6	1	
1	7	21	35	35	21	7	1	
1	8	28	56	70	56	28	8	1	
1	9	36	84	126	126	84	36	9	1	

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值