Thread: Counting reapetance of a word in a string

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    14

    Counting reapetance of a characters in a string

    Dear Friends, is there a premade function to count reapetance of a characters in a string or an array in C? if not what is your suggested approach to do it?

    for example:

    the input is: ARIZONA
    Result is : 2111112

    input is: KISS
    output : 1122


    Thanks.
    Last edited by Saeid87; 06-11-2009 at 01:07 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you are trying to count the frequency of characters, not words.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    14
    Edited the title..thanks for informing

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, a simple solution, assuming 8-bit bytes, is to create an array of 256 integers. These will be the frequencies, with the values of the characters as the corresponding indices. As such, you initialise this array to contain zeroes, then loop over the string and directly increment the corresponding frequency. Note that you may need to cast the char to unsigned char since char can be signed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    14
    wow can you please break it down a little bit? I could not get the idea maybe a little example please

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    I had this project in school as well.

    Think of it as having a place in an integer array for each of letter of the alphabet(A = 0, B = 1, C = 2, etc...).
    Iterate through the word once adding 1 to that letters index.
    Once you're all the way through the word, go back to the start.
    Iterate through again, but this time instead of adding 1, just read the value in that letters index.

    It's easier to think about with just the alphabet, but much easier to actuall implement with all 256 characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM