Question about string, binary tree and hash table | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Question about string, binary tree and hash table

I'm making a program that takes words from a book and counts word repetitions and their location. The output will be these sorted words, their location and number of repetitions. I'm thinking of using binary tree and symbol table, but I have the impression that the hash table is more efficient for large amounts of data... what do you think? Another question is about strings in c. I want to allocate memory for a pointer called "world" which is a field of my struct. I tried to allocate inside the struct itself. For example: typedef struct No{ char *world: world = (char*) malloc(sizeof(10)); } but it seems that this is not working. What's the correct way to store a string that I don't know the length of? I'm starting in C ;-; help me please and thanks!

16th Dec 2022, 2:37 PM
Be Luchesse
Be Luchesse - avatar
2 Answers
+ 1
Use an inverted index to store all the data, it's what we use in information retrieval (for search engines). You must allocate the memory of the string when you use the struct because when you declare the struct you only need the declaration of the types. First: typedef struct No{ char *world; } Then you should allocate the memory for the struct and the memory for the string. Another way to do it (more safely, but using more memory) is to give a specific size for the strings. typedef struct No{ char world[30]; }
1st Jan 2023, 9:25 PM
Edgar Sabido
Edgar Sabido - avatar
0
Thank you, I got it!
15th Jan 2023, 12:33 PM
Be Luchesse
Be Luchesse - avatar