# counting letter occurences in a string

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 04-30-2008
pjr5043
counting letter occurences in a string
hey guys, i have an extra credit assignment that wants me to count how many times each letter occurs in a string, i'm not sure how to get started:

heres the description, it doens't seem like it will be real difficult, but i'm having trouble getting started

Write a function that counts the occurance of each letters occurance in a string.
The function should recieve as formal paramater only an array of characters.

void count(const char myStr[])

For the function call

Only the following output should be produced :
The letter A occurs 3 times
The letter B occurs 2 times
The letter C occurs 1 times
The letter D occurs 4 times
The letter E occurs 5 times
The letter F occurs 2 times
The letter G occurs 1 times
The letter J occurs 1 times
The letter K occurs 1 times
The letter T occurs 4 times
The letter W occurs 1 times
• 04-30-2008
Daved
Do you know maps? That's what I would use.

If you haven't learned those yet you can also use an array and find a way to convert a character to an index of the array. The array would then hold the count of each character. As you loop through the characters in the input string, you just convert the character to an index and increment that index's count.
• 04-30-2008
pjr5043
i do not konw maps .. so yea .. that indexing method seems like it would work better. would i need to write something that places A into index 1 and B into index 2 and so on .. and then count each index?
• 04-30-2008
anon
Except that the keys of the maps would be sequential (chars), so you could just use an array.

The mapping isn't difficult either: you do know that char's are an integral type?

May-be this can enlighten you:
Code:

```#include<iostream> int main() {     for (unsigned i = 32; i != 128; ++i)         std::cout << i << " maps to " << (char)i << '\n'; }```
• 04-30-2008
cpjust
I'd use a std::map, but this should work almost as well as a map:
Code:

`unsigned int asciiTable[256] = { 0 };`
• 04-30-2008
Daved
>> would i need to write something that places A into index 1 and B into index 2 and so on ..
Yes, although I would put 'A' into index 0 since arrays start at 0.

It would be a simple formula that works on all characters (and you might need a separate forumla for upper versus lower case characters). You could also use a big array like cpjust suggested so that you don't need to come up with that formula, but you'd have to do a little extra work later to output only the counts of the characters.
• 04-30-2008
pjr5043
how do i assign a letter to an index in an array? how do i assign a to index 0?
• 04-30-2008
Daved
You have to come up with a formula to convert it. anon gave a hint earlier, a char variable is really just an integer code for a certain letter, so you need to convert that integer code to the integer index.
• 04-30-2008
cpjust
Quote:

Originally Posted by Daved
You could also use a big array like cpjust suggested so that you don't need to come up with that formula, but you'd have to do a little extra work later to output only the counts of the characters.

Actually, since the specific letters that you want to count are passed to the function, it should be as simple as using this in a loop:
Code:

`unsigned int count = asciiTable[ myStr[i] ];`
• 04-30-2008
Daved
The output should be in alphabetic order, so I don't think that would work exactly right.
• 04-30-2008
brewbuck
Quote:

Originally Posted by Daved
The output should be in alphabetic order, so I don't think that would work exactly right.

The moment somebody mentions "alphabetical" we're already locked in to a specific encoding. Without knowing the encoding there is no way to define what "alphabetical" even means. So if an encoding is to be assumed, we might as well assume ASCII, and use the "subtract 'A' " trick.
• 04-30-2008
cpjust
Quote:

Originally Posted by Daved
The output should be in alphabetic order, so I don't think that would work exactly right.

I didn't see Alphabetic Order as one of the requirements; only that they need to be in the array of characters passed to the function.
• 04-30-2008
Daved
>> I didn't see Alphabetic Order as one of the requirements
The expected output is listed in the original post, and it is in alphabetical order.

>> So if an encoding is to be assumed, we might as well assume ASCII, and use the "subtract 'A' " trick.
Yes, that's the idea that we're hoping the OP figures out. This appears to be a simple homework assignment, I doubt it's necessary to handle character sets other than ASCII.
• 04-30-2008
cpjust
Quote:

Originally Posted by Daved
>> I didn't see Alphabetic Order as one of the requirements
The expected output is listed in the original post, and it is in alphabetical order.

OK, for some reason I thought the input string was the list of characters to search for...
In that case, the example also counts letters without regard to case, so you'd have to use toupper() or tolower() on each character first before counting them.

Also, to really impress the teacher, you could use the std::count_if() function.
• 05-01-2008
pjr5043
alrite .. well don't i want to do something like this ..

*Index letter A to 0*
*Index letter B to 1*
*Index letter C to 2*
*so on, so on until Z*

then

*count Index 0 *
*count Index 1*
*count Index 2*
*etc, etc, etc*

then

*cout << count index 0*
*cout << count index 1*
*So on, so on, etc, etc*

that is how i am thinking of this in my head .. is that the correct way of accomplishing this project?
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last