Solved

Most frequently occuring characters

Posted on 2003-11-09
24
577 Views
Last Modified: 2013-12-03
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?
0
Comment
Question by:Tabris42
  • 11
  • 6
  • 5
  • +2
24 Comments
 
LVL 9

Assisted Solution

by:tinchos
tinchos earned 250 total points
ID: 9711880
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 Comment

by:Tabris42
ID: 9711900
It's not so simple as solving the alphabet. Numbers and capitals must also be considered.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9711912
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
 

Expert Comment

by:rishisk
ID: 9711921
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
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 9711952

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
 
LVL 9

Expert Comment

by:tinchos
ID: 9711964
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 Comment

by:Tabris42
ID: 9712105
Still pretty confused, buy thanks for giving me a starting point. I'm not too clear on how these methods will work.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9712118
What do you mean with

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

maybe we can still help
0
 

Author Comment

by:Tabris42
ID: 9712140
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9712170
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 Comment

by:Tabris42
ID: 9712209
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9712232
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Tabris42
ID: 9712245
Ok, now I understand. Thanks for all of your great help!
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9712252
Glad it helped

Tincho
0
 

Expert Comment

by:john2789
ID: 9719631
why does it need 256?
0
 

Expert Comment

by:john2789
ID: 9719636
what is 256? Is it 256 MB on PC memory?
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9719647
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
 

Expert Comment

by:john2789
ID: 9719680
What is the ascii code?
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9719688
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
 

Expert Comment

by:john2789
ID: 9719698
where is the 256 coming from?
0
 

Expert Comment

by:john2789
ID: 9719708
which one is the function and protype on this example?
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9719711
sorryp?¿
0
 

Expert Comment

by:john2789
ID: 9719727
which one is the function and prototype on this example?
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9719736
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now