# 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?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi Tabris42,

My idea would be the following

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

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

Hope this helps

Tincho
0
Author Commented:
It's not so simple as solving the alphabet. Numbers and capitals must also be considered.
0
Commented:
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
0
Commented:
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.
0
DBACommented:

Hi tabris42,

This is a pretty common question here on the boards.  One of the fun things about it is that it's a good exercise in learning C and it's also utilizes several techniques that have lots of real-world applications.

Tincho's gotten you on the right track, but the solution isn't complete.

1)  Declare your array to be 256 elements.  That's one position for each ASCII character.  There's no guarantee that the most commonly occurring character will be a letter, a digit, or anything else so you need to count parenthesis, periods, spaces, and every other character.
2)  Initialize the array to all zeros.
3)  Count each character in the string without regard to what kind of character it is.  Do this by incrementing the correct position in the array.
4)  Loop through the array and save the highest value.
5)  Loop through the array again and print ALL the characters that occurred the number of times as the value you found in the previous step.

Good Luck,
Kent
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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
0
Author Commented:
Still pretty confused, buy thanks for giving me a starting point. I'm not too clear on how these methods will work.
0
Commented:
What do you mean with

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

maybe we can still help
0
Author Commented:
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.
0
Commented:
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
0
Author Commented:
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

0
Commented:
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
0
Author Commented:
Ok, now I understand. Thanks for all of your great help!
0
Commented:

Tincho
0
Commented:
why does it need 256?
0
Commented:
what is 256? Is it 256 MB on PC memory?
0
Commented:
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
0
Commented:
What is the ascii code?
0
Commented:
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
0
Commented:
where is the 256 coming from?
0
Commented:
which one is the function and protype on this example?
0
Commented:
sorryp?¿
0
Commented:
which one is the function and prototype on this example?
0
Commented:
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
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.