Solved

Most frequently occuring characters

Posted on 2003-11-09
24
581 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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:
Kent Olsen 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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Finding a good hash function 4 127
Installing Fonts via Group Policy not Successful 10 99
What is atomic operation? 6 64
Please explain C language error codes ? 5 25
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

791 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