简体   繁体   中英

C program that finds out 2 most common letters in a string, and reverses them in the same string

This is what I have at the moment:

#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;
}

This basically lets me check how many letters are in the whole. I tried searching the whole internet to check how can I find the 2 most frequent, and reverse them in the string*. Am I doing something wrong? How can I improve?

(When I say reverse them in a string, I mean that instead of " i love this game i do i do " there will be " o live thos game o di o di" since "i" is the most frequent and "o" is the second most.)

here is sample, how you can count 2 most frequent symbols:

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);

One proposal, the idea is to:

  1. compute frequencies
  2. sort the results with qsort and a comparator function
  3. swap the 2 characters in the string

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM