I want to count frequency or occurrence of a every letter in a string C program

≯℡__Kan透↙ 提交于 2019-12-08 14:24:02

问题


Suppose if I pass a string like "I am Programmer".

If a letter has occurred one time it should print "I has occurred 1 time", or else if a letter appears twice in the string it should print "a has occurred 2 times", "m has occurred 3 times" and so on for every letter in the string. I searched it and found in some website. Is there any way we could rewrite the code because I didn't understand the code.

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

int main()
{
   char string[100];
   int c = 0, count[26] = {0};

   printf("Enter a string\n");
   gets(string);

   while (string[c] != '\0')
   {
      /** Considering characters from 'a' to 'z' only
          and ignoring others */

      if (string[c] >= 'a' && string[c] <= 'z') 
         count[string[c]-'a']++;

      c++;
   }

   for (c = 0; c < 26; c++)
   {
      /** Printing only those characters 
          whose count is at least 1 */

      if (count[c] != 0)
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }

   return 0;
}

回答1:


Ok here is the rewrite, the original code is better but this one might be easier to understand:

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

int main()
{
   char cur_char;
   char string[100];
   int index = 0, count[255] = {0};

   printf("Enter a string\n");
   gets(string);


   while (string[index] != '\0')
   {
      char cur_char = string[index];

      // cur_char is a char but it acts as the index of the array like
      // if it was an unsigned short
      count[cur_char] = count[cur_char] + 1;

      index++;
   }

   for (index = 0; index < 255; index++)
   {
      if (count[index] != 0)
         printf("%c occurs %d times in the entered string.\n", index, count[index]);
   }

   return 0;
}



回答2:


A variable type char can be considered as an integer (it's how they are stored in the memory anyway) so you can write:

int test = 'a';
printf("%i", test);

And it will print you 97. Also letters from a to z are represented by continuous intergers, that means 'b' = 98. So taht also means 'b' - 'a' = 1

In your solution, they create an array of 26 integers to count the occurence of each letters betwin 'a' and 'z' (note that they ignore all others including A-Z by doing this)

They decided that in the array count, index 0 is here to count occurences of a, 1 for b .... 25 for z, that explains this:

count[string[c]-'a']++;

If string[c] is a b then string[c]-'a' = 1 so we have our index for count array and increase the amount of occurence of b.

So all you need to understand this code is that you can manipulate a char like an int basically, you should make a quick search about what is ASCII code as well.

If you still need a rewrite of this code to understand, tell me.



来源:https://stackoverflow.com/questions/29845805/i-want-to-count-frequency-or-occurrence-of-a-every-letter-in-a-string-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!