• 作业十二 字符串和结构


    找出最长的字符串

    
    

    分类统计字符个数

    #include<stdio.h>
    int main()
    {
        int a,zm=0,kg=0,sz=0,qt=0;
        
        while((a=getchar())!='
    ')
        {
            if((a>='A'&&a<='Z')||(a>='a'&&a<='z'))
                zm++;
            else if(a==' ')
                kg++;
            else if(a>='0'&&a<='9')
                sz++;
            else
                qt++;
        }
        printf("英文字母的个数:%d
    ",zm);
        printf("空格的个数:%d
    ",kg);
        printf("数字的个数:%d
    ",sz);
        printf("其他字符的个数:%d
    ",qt);
        
        return 0;
    } 

    学生信息管理系统

    #include<stdio.h>
    #include<string.h>
    
    struct students_list{
        char name[10];
        int chinese,math,english;
        char ID[13];
        double average;
    };
    
    int Count=0;
    void new_student(struct students_list students[]);
    void search_student(struct students_list students[],char *name);
    void print_student(struct students_list students[]);
    int main()
    {
        int choice;
        char name[10];
        struct students_list students[50];
        do{
            printf("******学生信息查询******
    ");
            printf("	1:输入
    ");
            printf("	2:查询
    ");
            printf("	3:输出
    ");
            printf("	0:退出
    ");
            printf("请选择功能:");
            scanf("%d",&choice);
            switch(choice){
                case 1:
                    new_student(students);
                    break;
                case 2:
                    printf("请输入要查找的学生姓名:");
                    scanf("%s",name);
                    search_student(students,name);
                    break; 
                case 3:
                    print_student(students);
                    break;
                case 0:
                    break;
            }
        }while(choice!=0);
        printf("谢谢使用学生信息查询系统!
    ");
        return 0; 
    }
    void new_student(struct students_list students[])
    {
        struct students_list f;
        if(Count==50){
            printf("信息列表已满!
    ");
            return;
        }
        printf("请输入新学生的姓名:");
        scanf("%s",f.name);
        printf("请输入新学生的语文成绩:");
        scanf("%d",&f.chinese);
        printf("请输入新学生的数学成绩:");
        scanf("%d",&f.math);
        printf("请输入新学生的英语成绩:");
        scanf("%d",&f.english);
        printf("请输入新学生的学号:");
         scanf("%s",f.ID);
         students[Count]=f;
             Count++;
    }
    void search_student(struct students_list students[],char *name)
    {
        int i,flag=0;
        if(Count==0){
            printf("学生信息列表是空的!
    ");
            return;
        }
        for(i=0;i<Count;i++)
            if(strcmp(name,students[i].name)==0){
                flag=1;
                    break;
            }
            if(flag){
                printf("姓名:%s	",students[i].name);
                printf("语文成绩:%d	",students[i].chinese);
                printf("数学成绩:%d	",students[i].math);
                printf("英语成绩:%d	",students[i].english);
                printf("平均成绩:%.2lf	",students[i].average=(students[i].chinese+students[i].math+students[i].english)/3.0);
                printf("学号:%s
    ",students[i].ID);
            }
            else
                printf("无此学生信息!");
    }
    void print_student(struct students_list students[])
    {
        printf("学生信息列表的学生信息如下:
    ");
        int i;
        for(i=0;i<Count;i++)
        {
            printf("%s	%d	%d	%d	%.2lf	%s	
    ",students[i].name,students[i].chinese,students[i].math,students[i].english,students[i].average,students[i].ID);
        }
    }

     

    分类统计字符个数方法二

    
    
    #include<stdio.h>
    int main()
    {
        int digit,capital,letter,blank,other;
        char s[80];
        int i;
        digit=capital=letter=blank=other=0;
        
        printf("Enter characters:");
        gets(s);
        i=0;
        while(s[i]!='')
        {
            if(s[i]>='a'&&s[i]<='z')
                letter++;
            else if(s[i]>='A'&&s[i]<='Z')
                capital++;
            else if(s[i]>='0'&&s[i]<='9')
                digit++;
            else if(s[i]==' ')
                blank++;
            else
                other++;
            i++;
        }
         printf("大写字母的个数为:%d
    ",capital);
         printf("小写字母的个数为:%d
    ",letter);
         printf("数字的个数为:%d
    ",digit);
         printf("空格的个数为:%d
    ",blank);
         printf("其他字符的个数为:%d
    ",other);
        return 0;
    }
    
    
    
    
    

    找出最长的字符串修正

    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        int i;
        char sx[80],smlen[80];
        
        printf("请输入5个字符串:");
        scanf("%s",sx);
        strcpy(smlen,sx);
        for(i=1;i<5;i++)
        {
            scanf("%s",sx);
            if(strlen(smlen)<strlen(sx))
                strcpy(smlen,sx);
        }
        printf("maxlen is %s
    ",smlen);
        
        return 0;
    }

  • 相关阅读:
    eclipse中统计代码行数
    vmware ubuntu 最近遇到的一些问题
    Shows how to create and run composites dynamically
    程序员的艺术:排序算法舞蹈
    Apache Mina 网络通信
    OutOfMemoryError汇总
    c++/java/c# 几种编程语言的指针、引用比较
    Silverlight开发工具集合
    web.cong 详解
    ASP.NET中的web.config配置
  • 原文地址:https://www.cnblogs.com/cs-16-4-zlt/p/6160634.html
Copyright © 2020-2023  润新知