C程序,在字符串中找出2个最常见的字母,并在同一个字符串

时间:2017-01-13 17:57:17

标签: c

这就是我现在所拥有的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENG_LETTERS 26
#define STR_LEN 100

int main()
{
    int countArray[ENG_LETTERS] = {0};
    char str[STR_LEN] = { 0 };
    char engChar = 'a' - 1;
    int length = 0;
    int i , j = 0;
    int max = 0;
    int index = 0;
    printf("Please enter a string:\n");
    fgets(str , STR_LEN , stdin);
    length = strlen(str);
    for(i = 0 ; i <= length;i++)
    {
        if(str[i] == '\n')
        {
            str[i] = '\0';
        }
    }
    for(i = 0;i < ENG_LETTERS;i++)
    {
        engChar++;
        for(j = 0;j <= length - 1;j++)
        {
            if(str[j] == engChar)
            {
                countArray[i]++;
            }
        }
    }
    engChar = 'a'- 1;
    for(i = 0; i <= ENG_LETTERS - 1;i++)
    {
        engChar++;
        printf("There are %d %c letters\n", countArray[i],engChar);

    }

    system("PAUSE");
    return 0;
}

这基本上让我可以检查整个字母的数量。我试着搜索整个互联网,检查我如何找到最常见的2,并在字符串*中反转它们。难道我做错了什么?我该如何改进?

(当我说在一个字符串中反转它们时,我的意思是代替“我喜欢这个游戏,我会这样做”会有“o live thos game o di o di”,因为“i”是最常见的“o”是第二位。)

2 个答案:

答案 0 :(得分:0)

这里是示例,如何计算2个最常见的符号:

int freq[ENG_LETTERS] = { 0 };
char str[STR_LEN] = { 0 };
char *pStr = str;
int i, max, max2; // max - most frequent one, max2 - second most frequent

printf("Please enter a string:\n");
fgets(str, STR_LEN, stdin);

while (*pStr) {
    if ((*pStr >= 'a') && (*pStr <= 'z'))
        ++freq[(*pStr) - 'a']; // convert from symbol to index
    ++pStr;
}

max = max2 = 0; // lets assume that first letter (a) is most frequent one
for (i = 1; i < ENG_LETTERS; ++i) {
    if (freq[i] > freq[max]) {
        max2 = max; max = i; // save old max and update new max
    } else if (freq[i] > freq[max2])
        max2 = i;
}
max += 'a'; // convert indexes back to symbols
max2 += 'a';
printf("%c %c\n", max, max2);

pStr = str; // swap max symbols
while (*pStr) {
    if (*pStr == max) *pStr = max2;
    else if (*pStr == max2) *pStr = max;
    ++pStr;
}
printf("%s\n", str);

答案 1 :(得分:0)

一个建议,想法是:

  1. 计算频率
  2. 使用qsort和比较器函数对结果进行排序
  3. 交换字符串中的2个字符

    static int frequencyComparator (void const *a, void const *b)
    {
        const Histo* ha=a;
        const Histo* hb=b;
        return hb->freq-ha->freq;
    }
    
    struct Histo
    {
        char c;
        int freq;
    };
    
    Histo histo[ENG_LETTERS]={0};
    char str[STR_LEN] = { 0 };
    char *pStr = str;
    printf("Please enter a string:\n");
    fgets(str, STR_LEN, stdin);
    
    int index=0;
    while(*pStr!=0)
    {
        index=(*pStr)-'a';
        if(index>=0 && index<26)
        {
            histo[index].c=(*pStr);
            histo[index].freq++; 
        }
        pStr++;
    }
    
    qsort (histo, ENG_LETTERS, sizeof(Histo), frequencyComparator);
    
    if(histo[0].freq>0 && histo[1].freq>0)
    {
        printf("%c %c\n", histo[0].c, histo[1].c);
        pStr = str;
        while(*pStr!=0)
        {
            if(*pStr==histo[0].c) *pStr=histo[1].c
            else if(*pStr==histo[1].c) *pStr=histo[0].c
            pStr++;
        }
    }
    
    printf("%s\n", str);