Link to home
Start Free TrialLog in
Avatar of Tabris42
Tabris42Flag for United States of America

asked on

Most frequently occuring characters

I'm relatively new to C, and am not sure where to start with tackling this problem. I have to write a program that will read in a character string, and then print back the most commonly occurring character in the string, and how many times it has occurred. For example:

a man a plan a canal panama
The most frequently occurring character is 'a', and it occurs 10 times.

Any ideas on how I should approach this problem?
SOLUTION
Avatar of tinchos
tinchos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tabris42

ASKER

It's not so simple as solving the alphabet. Numbers and capitals must also be considered.
Avatar of tinchos
tinchos

Ok, then..........

1) declare a new array of ints with 37 positions (one for each letter and one for each digit)
2) in this case, position 0 will represent the number of times of letters A or a
                 position 1 will represent the number of times of letters B or b
and so on..............

3) initialize all elements in 0
4) foreach letter if it is an a or an A, increase position 0, if letter is b or B increase position 1 and so on..........
5) find the highest value of the array.

I guess this is what you meant.........

If not, please let me know

Tincho
If you are going to consider non-alphabetic characters (like spaces, tabs ...)  too, then you should be having an array of 256, in which case, use

count[ch]++;

where count and ch are declared as

unsigned int count [256] = {0}; /* to initialize the elements to zero */
unsigned char ch;

After this follow step 5 of Tincho's approach.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok

My answer was because I considered that the string would be containing letters (in the first case), or letters + numbers (in the second one).

Just so sum up, I guess that rishisk and kdo's ideas and mine are all the same.

I guess there's little more to add

Tincho
Still pretty confused, buy thanks for giving me a starting point. I'm not too clear on how these methods will work.
What do you mean with

"I'm not too clear on how these methods will work."

maybe we can still help
Well, steps 1 and 2 were easy enough, but steps 3-5 have me baffled.
Here's the code I have now:

    int i;
    int characters[256];
    char string[MAX];
    for (i=0; i<256; i++){
        characters[i] = 0;
    }
    printf("Enter string:\n");
    gets(string);

    printf("The most frequently occuring character is .\n");
    printf("The character occurs times.\n");

But I'm confused about what's next? I don't know how to count through each character, or what exactly the process behind counting the occurrences of each character would be.
First of all

you can change

    int i;
    int characters[256];
    char string[MAX];
    for (i=0; i<256; i++){
        characters[i] = 0;
    }

with

    int i;
    int characters[256] = {0};      // All elements will be initialized to 0
    char string[MAX];

Then you have the string stored in string.

then you have

characters[0] = number of times that character whose ascii code is 1 appears
characters[1] = number of times that character whose ascii code is 2 appears
characters[10] = number of times that character whose ascii code is 11 appears
characters[33] = number of times that character whose ascii code is 34 appears
and so on

so you can do........

    gets(string);

    /* For all the string process each character */
    for( i = 0; string[i] != '\0'; i++ )
    {
          characters[ string[i]-1 ]++;      // For example, if string[i] is 'a', then it will increase the times 'a' appears in the characters string
    }

   maxCharacter = findMax( characters );

    printf("The most frequently occuring character is %c\n", maxCharacter  );
    printf("The character occurs times %d.\n", characters[maxCharacte] );


Now you just have to implement findMax that returns the character which appears most times

it's prototype would be something like

char findMax( char *string );

Hope this helps

Tincho
That looks good, but I don't understand this line:

          characters[string[i]-1]++;

an array inside an array? forgive me if I sound new, because I am. :P


No, the idea is the following

I don't have here the ascii table but suppose the following

'A' = (char)65
'B' = (char)66

and so on

then in characters[ 65 - 1 ] will be stored all the times that 'A' appears
in characters[ 66 - 1 ] will be stored all the times that 'B' appears

and so on

in this way................

it is the same to say

characters['A'] than to say characters[65]

then, if suppose that in string[i] an 'A' appears then string[i] = 65

then characters[ string[i] - 1 ] ++ is the same than

characters[ 65 - 1 ]++

which is the same than

characters[64]++;

which is the place where will be stored all the times that 'A' appears

Hope this is clear enough

Tincho
Ok, now I understand. Thanks for all of your great help!
Glad it helped

Tincho
why does it need 256?
what is 256? Is it 256 MB on PC memory?
256 is the number of elements of the array

it means the number of different characters you can enter

Remember that there are 256 ascii codes?

Hope this helps

tincho
What is the ascii code?
I mean that there are only 256 possible different characters that you can type

each of the represented by a number from 0 to 255

for example

the character represented by 65 is 'A'
the character represented by 66 is 'B'
the character represented by 67 is 'C'

in order to see them (some of them are not visible)

try with this

int i;

for( i = 0; i < 256; i++ )
{
     printf( "%c - %d\n", (char) i, i );
}

Hope this helps

tincho
where is the 256 coming from?
which one is the function and protype on this example?
sorryp?¿
which one is the function and prototype on this example?
There is no function and no prototype

if you have a function

int f()
{
}

this is the function

and

int f();

this is the prototype

Tincho