• linux实现针对文本统计字母出现的次数(所有的可打印的字符)


    最近在看看有意思的编程,发现算法真是一个好东西,呵呵,自己也写了一个简单的demo

    代码具体如下:

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdint.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #define bSize 255
    #define bufSize 4096
    static uint64_t  arr[255] ={0};
    static uint64_t  hash = 0;
    void whash(char *s)
    {
        int len = strlen(s);
        int i =0,total =0;
        while(i < len) {
            int index = s[i] -32;
            if((hash &(1 <<index)) ==0) {
                hash |= (1 <<index);
                arr[index]++;
            } else {
                arr[index]++;
            }
            i++;
        }
    }
    int bread(char *file)
    {
        int fd = open(file,O_RDONLY,0666);
        struct stat st;
        char buf[bufSize] = {''};
        if(fd == -1) {
            return -1;
        }
        fstat(fd,&st);
        size_t  total = st.st_size;
        while(total > 0) {
            int left =read(fd,buf,bufSize);
            if(left < 0) {
                break;
            }
            whash(buf);
            total -= left;
            memset(buf,'',bufSize);
        }
        return 0;
    }
    int main(void) {
        printf("bread = %d
    ",bread("./sry.c"));
        int i = 0;
        for(;i < bSize;i++) {
            if(arr[i] > 0){
                printf("count(%c) =%d
    ",i+32,arr[i]);
            }
        }
        return 0;
    }

    运行结果  如下:

    zhoulin@:~/code_c_20160101/algorithm/str:./sry
    bread = 0
    count( ) =335
    count(") =6
    count(#) =9
    count(%) =3
    count(&) =2
    count(') =4
    count(() =23
    count()) =23
    count(*) =2
    count(+) =9
    count(,) =11
    count(-) =4
    count(.) =10
    count(/) =3
    count(0) =15
    count(1) =4
    count(2) =4
    count(3) =2
    count(4) =3
    count(5) =4
    count(6) =6
    count(9) =1
    count(;) =27
    count(<) =14
    count(=) =19
    count(>) =9
    count(D) =1
    count(L) =1
    count(N) =1
    count(O) =2
    count(R) =1
    count(S) =6
    count(Y) =1
    count([) =7
    count() =4
    count(]) =7
    count(_) =5
    count(a) =28
    count(b) =14
    count(c) =17
    count(d) =29
    count(e) =48
    count(f) =28
    count(g) =1
    count(h) =22
    count(i) =61
    count(k) =1
    count(l) =23
    count(m) =3
    count(n) =41
    count(o) =10
    count(p) =4
    count(r) =31
    count(s) =33
    count(t) =55
    count(u) =23
    count(v) =2
    count(w) =4
    count(x) =5
    count(y) =4
    count(z) =8
    count({) =13
    count(|) =1
    count(}) =13
  • 相关阅读:
    PyCharm2019激活
    实时统计每天pv,uv的sparkStreaming结合redis结果存入mysql供前端展示
    Python批量删除mysql中千万级大量数据
    SpringBoot + thymeleaf 实现分页
    适合用设计模式解决的问题场景
    重新理解面向过程和面向对象编程
    优化前端单页面应用性能指南
    使用ts开发vue项目知识点1
    职场人的核心竞争力应该是什么
    重新理解原型链
  • 原文地址:https://www.cnblogs.com/innobase/p/5163747.html
Copyright © 2020-2023  润新知