当前位置:编程学习 > C/C++ >>

打印字符串中各个字符出现的频率的直方图

[cpp]
#include<stdio.h>  
#include<string.h>  
#include<stdlib.h>  
#include<ctype.h>  
#define N 256  
void printLetterFreq(char *line) 

    int hash[N] = {0}; //对应于ascII  
    int i, j; 
    for (i = 0; i < strlen(line); i++) 
    { 
        hash[line[i]]++; //计算相同字符个数  
    } 
 
    for (i = 0; i < N; i++) 
    { 
        if (hash[i])  //该字符出现过  
        { 
            if(isspace(hash[i])==0&&isblank(hash[i])==0) 
            { 
                printf("%c|-", i); 
                for (j = 0; j < hash[i]; j++) //字母出现的次数  
                printf("*"); 
                printf("\n"); 
            } 
        } 
    } 

int main() 

    char p[]= 
"To recap, the three main objectives in the Mystery Method are: \ 
To attract a woman \ 
To establish comfort, trust, and connection \ 
To structure the opportunity to be seduced "; 
    printLetterFreq(p); 
    return 0; 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,