C code to Reverse words in a string

When it comes to C programming, the saying “Practice make perfect” is very much valid.
So here I am sharing a sample code to reverse words within a sample string.
Since this program does not belong specifically to the “Embedded C” category, this post is not marked under the category.

Working of this program is pretty simple 🙂

The pseudo-code is as follows…

I. Read-in each character within the string to an array until a ‘space’ character is found

II. When a ‘space’ character is found; print the earlier read string in reverse order

III. Repeat step I and II until the entire string is scanned

Note: The “flag” variable is used to mark whether the scan is presently within a word or outside of a word.

The output of the above program: