I'm working on a project where I need to sort a given sentence based on ASCII values. However, my professor has laid down some strict rules: I can't use dynamic memory allocation, and the array size can't be predefined from the start. I thought about using variable-length arrays, but the issue is that I end up needing to input the sentence twice—once to gauge its size and again for the actual input. I'm currently using getchar and putchar for input and employing bubble sort to handle the sorting part. I've tried seeking help from AI, but it's been of no use. I'm really stuck and would appreciate any hints or advice you could share!
3 Answers
Honestly, it sounds like your professor wants you to implement a sort where you don’t rely on fixed sizes or allocators. Have you thought about using linked structures, or maybe reading character by character until a delimiter is hit, such as a newline? It would be a bit more complex but could help you bypass the double input issue since you wouldn't have to declare an array at all.
You might want to visualize the problem to better tackle it. Grab a whiteboard and sketch out how your inputs would change with sorting. Once you maybe rearrange them visually, it could clarify your approach and how you plan to compare and convert them using your bubble sort.
Since you can't use dynamic memory, you might consider setting a maximum character limit. A good upper bound could be around 10,000 characters—more than enough for most sentences. You should enforce this limit in your program and throw an error message if it gets exceeded. For the input issue, it’s common in exercises to first collect the size of the input, and then get the actual content. Just make sure you handle errors if the input size doesn't match the expected count, which could simplify your process a bit.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically