简体   繁体   中英

How do I calculate count times for every char array with struct?

struct Page{
 char data[50];
 int count = 0; // ?
};
Page page[50];

I don't know how to save count for every char array.

for exmaple:

input: a c b
page[i].data[0] = a; // a_count = 1
page[i].data[1] = c; // c_count = 1
page[i].data[2] = b; // b_count = 1

my ideas is

paga[i].data[0].count++;

but, I don't know how to implement with struct.

Instead of a single integer count , you need an array to solve this issue. So you can define Page in the following way:

typedef struct Page
{
    char data[50];
    int countChar[TOTAL_CHARS];
} Page;

Now, we can't simply initialize an array inside a struct like we can in a function. So we have to manually initialize countChar[] with 0 . However, there is a trick that can save you from this tiresome process. And the trick is to use a macro like this:

#define NEW_PAGE { "", {0} }

and use it in the following way:

Page page[50] = NEW_PAGE;

Now, all you have to do is map the character to the index of countChar[] and increment its value by 1. This can be done in the following way:

page[0].countChar[ch - 'a']++;

Here, ch is the character from input. Considering all the input will be lowercase letters, subtracting ch with 'a' will produce the required index that represent frequency of character ch . If the possible value of ch were all ASCII characters, we would simply replace 'a' with '\0' and change size of countChar[] accordingly.

Here's a code that tests this idea:

#include<stdio.h>
#define NEW_PAGE { "", {0} }
#define TOTAL_CHARS 26

typedef struct Page
{
    char data[50];
    int countChar[TOTAL_CHARS];
} Page;

int main()
{
    Page page[50] = NEW_PAGE;
    char input[] = "hello world";
    char ch;
    int i = 0;
    int frequency;

    while(input[i] != '\0')
    {
        ch = input[i];
        page[0].data[i] = ch;
        page[0].countChar[ch - 'a']++;
        i++;
    }

    for(i=0; i<TOTAL_CHARS; i++)
    {
        frequency = page[0].countChar[i];
        if(frequency != 0)
            printf("%c is present %d times\n", ('a'+i), frequency);
    }
    return 0;
}

Instead of having a single int , you could have an array of integers of length 26. This would hold the count of each lowercase letter in the array.

The integer values for lowercase letters range from 97 - 122 . To correspond each letter to an index in a 26 length array, you need to subtract 97 from the integer value of each char so that each char can correspond to an index from 0 - 25.

So your struct should look something like this:

struct Page{
 char data[50];
 int count[26];
};

If you had a char called letter , the way you would find the correct index of the count array and increment it would be:

int index = (int)letter - 97;
count[index]++;

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