I was practicing some programming problems and tried to code the popular "reverse words in a string" problem. 

I tried to come up with my own code in C. I am able to partially get it right. That is, "hello world" becomes "world olleh". I am wondering what the bug is here. I think somewhere I am creating an off by 1 bug. 

As much as possible, I wanted to do it without using library functions. I searched here for this problem &amp; found many solutions, but I'd like to know why my solution doesn't work. 

Here is the code:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

void reverse(char*, int);

int main(int argc, char **argv)
{
    char st[]= "hello world";
    int len = strlen(st);
    int i=0,j=0;

    reverse(st,len-1); // Reverse the entire string. hello world =&gt; dlrow olleh

    while(st[j]){ //Loop till end of the string
        if ( *(st+j) == ' ' || *(st+j) == '\0' ) { //if you hit a blank space or the end of the string
            reverse(st+i,j-1); // reverse the string starting at position i till position before the blank space i.e j-1
            i=++j; //new i &amp; j are 1 position to the right of old j
        }
        else {
            j++; //if a chacacter is found, move to next position
        }               
    }       

    printf("%s",st);
    return 0;
}

void reverse(char *s, int n)
{
    char *end = s+n; //end is a pointer to an address which is n addresses from the starting address
    char tmp;
    while (end&gt;s)  //perform swap
    {
        tmp = *end;
        *end = *s;
        *s = tmp;
        end--;
        s++;
    }
}


Thank you!

UPDATE: Based on @Daniel Fischer's answer, here is the correct implementation : http://ideone.com/TYw1k


Question: Moving user perspective in Blender with Python
Answer: You can use bpy.ops.view3d.view_pan() to do the same as Ctrl+Numpad #, e.g.:

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        override = {'area': area, 'region': area.regions[-1]}
        bpy.ops.view3d.view_pan(override, type='PANRIGHT')


The amount of the panning can't be controlled.

If you manipulate the view_matrix (or view_location etc.) directly, there's a lot more control. But you will have to calculate the proper matrices and vectors, and if something went wrong, you may end up with strange view or navigation reaction:

r3d = C.screen.areas[2].spaces[0].region_3d # region_3d of 3D View
r3d.view_matrix.col[3][1:4] # translation part of the view matrix


Have a look at the FPS Fly addon to see what can be done and how:
http://www.ewocprojects.be/fpsfly.html

AFAIK viewport navigation is a bit weird after use, as the addon might manipulate the wrong property to fly around. The C-implementation will be different.