如何打印字符串中字符的频率?

时间:2019-07-06 15:11:18

标签: c

假设输入为

  

以下代码显示了t1r1e2e2的输出。如何避免两次打印e2

int main() 
{
    char a[20];
    int count, j, i, k; //abcd
    gets(a);
    int p = strlen(a);

    for (j = 0; j < p; j++) {
        count = 0;
        for (k = 0; k < p; k++) {
            if (a[j] == a[k])
                break;
        }
        for (i = 0; i < p; i++) {
            if (a[j] == a[i]) {
                count++;
            }
        }
        printf("%c%d", a[j], count);
    }
}

2 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题

  • 请勿使用 gets ,它已被弃用,如果您输入的内容太长,则会以不确定的行为写出接收字符串
  • k 的循环没有用
  • 您使用 int ,但正确的类型是import statsmodels.formula.api as sm from statsmodels.tools.tools import add_constant from sklearn.datasets import load_breast_cancer import pandas as pd import numpy as np data = load_breast_cancer() x = data.data y= data.target x=add_constant(x,has_constant='add') model = sm.Logit(y, x).fit_regularized() margeff = model.get_margeff(dummy=True,count=True) ##print the margal effect print(margeff.margeff) >> [ 6.73582136e-02 2.15779589e-04 1.28857837e-02 -1.06718136e-03 -1.96032750e+00 1.36137385e+00 -1.16303369e+00 -1.37422595e+00 8.14539021e-01 -1.95330095e+00 -4.86235558e-01 4.84260993e-02 7.16675627e-02 -2.89644712e-03 -5.18982198e+00 -5.93269894e-01 3.22934080e+00 -1.28363008e+01 3.07823155e+00 5.84122170e+00 1.92785670e-02 -9.86284081e-03 -7.53298463e-03 -3.52349287e-04 9.13527446e-01 1.69938656e-01 -2.89245493e-01 -4.65659522e-01 -8.32713335e-01 -1.15567833e+00] # manual calculation, doing this as you can get the coef_ from a sklearn model and use in the function def PDF(XB): var1 = np.exp(XB) var2 = np.power((1+np.exp(XB)),2) var3 = (var1 / var2) return var3 arrPDF = PDF(np.dot(x,model.params)) ME=pd.DataFrame(np.dot(arrPDF[:,None],model.params[None,:])) print(ME.iloc[:,1:].mean().to_list()) >> [0.06735821358791198, 0.0002157795887363032, 0.012885783711597246, -0.0010671813611730326, -1.9603274961356965, 1.361373851981879, -1.1630336876543224, -1.3742259536619654, 0.8145390210646809, -1.9533009514684947, -0.48623555805230195, 0.04842609927469917, 0.07166756271689229, -0.0028964471200298475, -5.189821981601878, -0.5932698935239838, 3.229340802910038, -12.836300822253634, 3.0782315528664834, 5.8412217033605245, 0.019278567008384557, -0.009862840813512401, -0.007532984627259091, -0.0003523492868714151, 0.9135274456151128, 0.16993865598225097, -0.2892454926120402, -0.46565952159093893, -0.8327133347971125, -1.1556783345783221] ,因为 int 可能不够大

一种方法是修改字符串,假设您不想计算换行符,并且还将打印结果的方式更改为更具可读性:

.get_margeff()

编译和执行:

size_t

如果要管理所有字符,包括换行符,则可以为循环存储字符串的初始长度,并使用字符0标记已计数的字符。当然,如果您不需要修改原始字符串,只需对其副本进行处理,或者使用包含字符计数的附加数组(索引是字符代码,警告字符是否已签名),或者检查是否当前字符在前一个字符中

答案 1 :(得分:-1)

这是另一个。只需校准过程功能即可处理任意数量的数据。初始化函数将计数器清零。例如,它还会打印不可打印的字符十六进制数字'\x0a'

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

static unsigned freqtable[256];

void init(void)
{
    memset(freqtable, 0, sizeof(freqtable));
}

unsigned *process(const unsigned char *buff, size_t size)
{
    while(size--)
    {
        freqtable[*buff++]++;
    }
    return freqtable;
}

void print(void)
{
    for(unsigned i = 0; i < 256; i++)
    {
        if(freqtable[i])
        {
            if(ispunct(i) || isalnum(i))
            {
                printf("%c%u", i, freqtable[i]);
            }
            else
            {
                printf("'\\x%02x%u", i, freqtable[i]);
            }
        }
    }
    printf("\n");
}

#define T1 "tree"

#define T2 "\r\nHello world \r\n\r\n"

int main()
{
    init();
    process(T1, strlen(T1));
    print();
    process(T2, strlen(T2));
    print();
    init();
    process(T2, strlen(T2));
    print();    

    return 0;
}