简体   繁体   中英

Program that detects which letter is the most common

I am a beginner in C, I am trying to make a program which detects which letter is the most common of max 10 letters. Here is what I've got so far:

char one = 'a'; //0110 0001
char check[10];

scanf("%s", &check);
char *ptr;
int a = 0;int b = 0;int c = 0;int d = 0;int e = 0;int f = 0;int g = 0;int h = 0;int i = 0;int j = 0;int k = 0;int l = 0;int m = 0;int n = 0;int o = 0;int p = 0;int q = 0;int r = 0;int s = 0;int t = 0;int u = 0;int v = 0;int w = 0;int x = 0;int y = 0;int z = 0;


if (check[0]=='a'){
    a += 1;
    if (a> b && a> c && a> d && a> e && a> f && a> g && a> h && a> i && a> j && a> k && a> l && a> m && a> n && a> o && a> p && a> q && a> r && a> s && a> t && a> u && a> v && a> x && a> y ){
        printf("A is the most common letter);
    }
}

The 'if' statement is only for the first letter that is entered and it is only checking the letter a. Here is where I need help, how do I optimize that? How can I make a loop instead of having all that a>b && a>c ... etc. Also if it is possible to declare a lot of similar variables in a shorter way? Just generally how do I keep short and am I doing something wrong?

Thank you.

You can certainly do it your way and compare every letter with every other. But usually you do this in two phases

  • count how often the letters occur

     int letters[26]; int i, n = strlen(check), max; memset(letters, 0, sizeof(letters)); for (i = 0; i < n; ++i) { char c = tolower(check[i]); letters[c - 'a']++; } 
  • pick the highest one

     max = 0; for (i = 1; i < 26; ++i) if (letters[i] > letters[max]) max = i; printf("%c is the most common letter\\n", max + 'a'); 

Yes you are doing a lot of things wrong.

this part of code

int a = 0;int b = 0;int c = 0;int d = 0;int e = 0;int f = 0;int g = 0;int h = 0;int i = 0;int j = 0;int k = 0;int l = 0;int m = 0;int n = 0;int o = 0;int p = 0;int q = 0;int r = 0;int s = 0;int t = 0;int u = 0;int v = 0;int w = 0;int x = 0;int y = 0;int z = 0;

is something horrific.

Use an array, like that: int letter[25];

letter[0] will be your a , letter[1] will be your b ... letter[49] will be your z .

this part of code

if (check[0]=='a'){
    a += 1;

is a bad way to do what you want.

you should do it like this:

int i;

for(i = 0; i < 10; ++i)
{
    if(check[i] >= 'A' && check[i] <= 'Z') // Check if letter is uppercase.
        ++letter[check[i] - 'A']; // 'A' == 65, But our array is from 0 to 49.

    if(check[i] >= 'a' && check[i] <= 'z') // Check if letter is lowercase.
        ++letter[check[i] - 'a']; // 'a' == 97. Note that 'A' is not 'a'.
}

This will check which character is most common, and stores it in the letter array.

Impressive work :)

int counts[26]; // We are expecting 26 letters
char check[10];
char *ptr;
scanf("%s", check);

memset(counts, 0, sizeof(counts)); // zero all array values
for (ptr = check; *ptr; ptr++)
{
   char ch = *ptr;
   if (isalpha(ch)) // ignore non-alphas
   {
      ch = tolower(ch);
      counts[ch - 'a']++;
   }
}

You can add best index lookup code from dreamzor answer

You need to use loops and arrays. Just store the amount of letters you've got in an array and then compare current the letter amount to best amount:

char check[10];
scanf("%s", &check);
const int checkSize = strlen(check);

int numberOfLetters[26]; 
for(int i = 0; i < 26; ++i) 
    numberOfLetters[i] = 0;

int bestLetterIndex = -1;

for(int i = 0; i < checkSize; ++i) {
    int letterIndex = check[i] - 'a'; // getting index from ASCII code
    numberOfLetters[letterIndex]++;

    if(i == 0 || // first letter, index not found yet 
       numberOfLetters[letterIndex] > numberOfLetters[bestLetterIndex]) {
        bestLetterIndex = letterIndex;
    }
}

printf("Most common letter is %c", (char)(bestLetterIndex + 'a'));

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