Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please Help Me.....


I want to create a dictionary In C language..
like if I search 'Ab', then those words will display, which is starts from 'Ab'....
Posted
Comments
Zoltán Zörgő 21-Nov-13 8:26am    
What have you tried?
It depends on your requirements... if this is a homework or something, than you can make it quick and dirty. If it is something serious, than you will need an embedded database engine, but at least some indexing. What platform/OS are you targeting?

There are many possible ways. I'd consider building a tree where each leaf and branch corresponds to one character. e. g. if you have the words "dog", "donut" and "day", your tree would look something like this:
    [d]
    / \
  [a] [o]
  /   / \
[y] [n] [g]
    /
  [u]
  /
[t]

You'll need to consider how to set up the data structure for the nodes: obviously each can have up to 26 children (assuming you're just using a-z, no numbers, no special characters or accents), and you somehow need to mark those nodes that are actual words even tough they branch into more words (e. g. "do" is a valid word but branches into "dog" and "donut"):

When you want to print a list of valid words starting with "do", then you only need to traverse the tree to the leaf [d]-[o] and then traverse all children.

Note that this solution may be more complex to implement than solution 1, and adding a word to the dictionary may be slower with this solution. but looking up a range of words can be very fast if done right.
 
Share this answer
 
Comments
Theo Buys 25-Nov-13 9:12am    
Yes, a tree structure is here the best solution. Because you program in plain C, read about "Self-referential structures" in the C programming language by Kernighan and Ritchie. Put you words one by one into the tree. Your search function walks the tree to a node and complete the words from that node to the leaves.
You might mimic how the paper dictionaries are done: storing your strings in alphabetic order.
Then you could perform a binary search in order to find the first and last items of the string range satisfying the user input.
C++ libraries (if you can use such programming language) would help you.
 
Share this answer
 
have a look on google sparsehashmap for memory usage, densehashmap for performance. if the dictionary elements is limited, recommend densehashmap.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900