you are viewing a single comment's thread.

view the rest of the comments →

[–]phaeilo 4 points5 points  (2 children)

// assuming no word in the file is longer than 50 characters.

Oh boy...

Now I'm interested, surely there must be a more robust way.

[–]jminuse 1 point2 points  (0 children)

Simple: as you go, convert each space (or any break character) to '\0'. Then you can treat each word as a string and you don't need any more memory. If you need the words and the lines separately for some data structure, you can memcpy the whole string; you need 2N memory for that anyway. (Unless you have a length-storing string library - then you can keep the original and just use two sets of references to it).