counting letter occurences in a string

This is a discussion on counting letter occurences in a string within the C++ Programming forums, part of the General Programming Boards category; hey guys, i have an extra credit assignment that wants me to count how many times each letter occurs in ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    37

    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.
    Use the following header :


    void count(const char myStr[])


    For the function call

    count("ABcaBadd*eekjdfefdeg,TTew44Tt")

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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';
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'd use a std::map, but this should work almost as well as a map:
    Code:
    unsigned int asciiTable[256] = { 0 };

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    how do i assign a letter to an index in an array? how do i assign a to index 0?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    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] ];

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The output should be in alphabetic order, so I don't think that would work exactly right.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Daved View Post
    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.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    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.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    >> 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.
    Last edited by cpjust; 04-30-2008 at 05:26 PM.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 12:24 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 10:18 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21