Thread: String array

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    String array

    I have an array of string: char array[][]. I have sorted it in order and there are many duplicates.

    Is there a way to count the duplicate of every string and store them with string:

    e.g:
    array["help"]->2 //2=number of occurences of "help."

    Thanks,
    Mark

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yeah, but it's not simple. What it sounds like you want is a hashmap or hashtable. These are data structures where you can look up values based on arbitrary data types (like strings).

    This would allow you to do something like:
    Code:
    my_hashtable* ht = create_ht();
    ht_put("help", 2);
    int value = ht_get("help");
    The problem is that you need to implement the data structure yourself (or find an existing implementation online).

    Of course you could go the easy route, and just do something like:
    Code:
    struct s
    {
        char string[100];
        int num_duplicates;
    };
    
    // Now just create a linked list of these structures which tell you the string in 
    // question, and the number of duplicates
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    Thanks. I will look into it tomorrow.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by bithub View Post
    Yeah, but it's not simple. What it sounds like you want is a hashmap or hashtable. These are data structures where you can look up values based on arbitrary data types (like strings).

    This would allow you to do something like:
    Code:
    my_hashtable* ht = create_ht();
    ht_put("help", 2);
    int value = ht_get("help");
    The problem is that you need to implement the data structure yourself (or find an existing implementation online).

    Of course you could go the easy route, and just do something like:
    Code:
    struct s
    {
        char string[100];
        int num_duplicates;
    };
    
    // Now just create a linked list of these structures which tell you the string in 
    // question, and the number of duplicates
    An hashtable doesn't seem to be the best data structure for this. This seems like it needs something that in c++ would be called a multiset. If you just need to use something like that, use a multiset if you can use c++, otherwise search for a library that implements a similar data structure in c. If you need to write the data structure by yourself, you could do it for example with a binary tree holding a list on each node, if you absolutely need to store the copies, otherwise you could just put a counter together with the single stored value on each node.
    Last edited by MisterIO; 11-04-2009 at 05:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM