471,831 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

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


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
1 2327
9,065 Expert Mod 8TB
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.

Similar topics

8 posts views Thread by Steven | last post: by
7 posts views Thread by eric.gagnon | last post: by
3 posts views Thread by Jim Carlock | last post: by
19 posts views Thread by pkirk25 | last post: by
1 post views Thread by archanapatelwhite | last post: by
7 posts views Thread by Adrian | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.