Text Parsing Power

Parsing is an activity that programmers are often reluctant to do themselves. That’s because parsing can be a real pain in the rump. Not only that, but why write your own function when you can use specific libraries that handle the job for you? That’s a blessing, but it won’t let you escape from attempting this month’s Exercise.

The standard C library features several parsing functions. I’ll cover them later this month. You can also use libraries that parse specific types of data, such as XML, JSON, and so on. Even then, parsing isn’t always a breeze; with anything new you’ll find a learning curve. Still, it’s nice to have someone else do all the grunt work.

To review, parsing is the process of taking some form of organized data and pulling out relevant pieces.

For complex data, parsing might involve locating special markers, looking up keys or tokens, and then extracting what’s relevant.

For example, suppose your code needs to locate a line of text ROTATE=ON in a file. You must open the file, scan each line for ROTATE, and then evaluate the text after the equal sign, which could be ON or OFF or some other value.

This month’s Exercise uses parsing on a simple level. Your solution prompts the user to input text. The code examines the text, then outputs the words input in reverse order. Here’s sample output:

Type some text: A long time ago, in a galaxy far, far away
away far far galaxy a in ago time long A

The parsing part is finding the start — and end — of each word. For the above example, my solution located and saved the start of each word in an array. The array is then worked in reverse order to output the individual words one at a time. My solution also ignores punctuation, though your solution doesn’t have to.

The parsing exercise is not about reversing the text in the words or just outputting the entire string backwards. Instead, each word is displayed, which shows that the original string is searched, word boundaries are located, and then the text is output.

Multiple solutions exist to solve this problem, depending on your approach. I wrote three solutions: One uses pointers, another arrays, and a third kind of cheats, but that’s okay because it’s still a solution. Click here to see what I did, but please attempt this Exercise on your own before peeking at my solutions.

Leave a Reply