Untitled

 avatar
unknown
c_cpp
3 years ago
1.3 kB
4
Indexable
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

int len=0;

struct student{
  char name[500];
  int sum;
};

struct student ary[5000];

char input[500];

int cmp(const void* aa,const void*bb){
    const struct student* a = (const struct student*)aa;
    int aLen=strlen(a->name);
    const struct student* b = (const struct student*)bb;
    int bLen=strlen(b->name);
    return (strcmp((char*)a->name,(char*)b->name));
}

int main(){
    int total=0;
    while(scanf("%s",input)!=EOF){
        int len=strlen(input);
        for(int i=0;i<len;i++){
            input[i]=tolower(input[i]);
        }
        bool exist=false;
        for(int i=0;i<total;i++){
            if(strcmp(input,(char*)ary[i].name)==0){
                ary[i].sum++;
                exist=true;
                break;
            }
        }
        if(exist==false){
            int len=strlen(input);
            for(int i=0;i<len;i++){
                ary[total].name[i]=input[i];
            }
            ary[total].sum=1;
            total++;
        }
    }


    qsort(ary,total,sizeof(ary[0]),cmp);

    for(int i=0;i<total;i++){
        for(int j=0;j<strlen(ary[i].name);j++){
            printf("%c",ary[i].name[j]);
        }
        printf(" %d\n",ary[i].sum);
    }

}
Editor is loading...