By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,132 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,132 IT Pros & Developers. It's quick & easy.

Storing many strings, print alphabetical, print # of duplicates.

P: 1

I am after some advice about which data structures I should use. I'm developing a program and I am at the point where all the strings are being generated and printed one by one with loops/recursion using printf("%s\n", my_string);. Instead of printing them, I need to sort them in alphabetical order, and record the amount of times a unique string appears.

i.e it will print

"ABCD 4" because ABCD appeared 4 times.
"BCC 1" because BCC comes after ABCD in alphabetical order and appears once.

My idea is to incorporate a binary (search?) tree, adding the strings one by one, and then printing the tree using inorder traversal. How would I integrate the duplicate string counter though? I then thought perhaps insert each item into the tree as a structure containing an int and a string, and everytime I insert a string, I should check for duplicates, and if found, update that int in the appropriate node's structure. I'm not sure if this is all that efficient. I have a feeling there's an easier and more efficient way, but I can't think of anything better.

Any input/advice/suggestions from anybody is greatly appreciated.

Sep 8 '06 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 8,968
Your idea sounds about right to be.
Sep 8 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.