lol, can't understand one C++ program written by myself (from cracking the coding interview)
===

Here is the question from "cracking the coding interview" Q1.3 "Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not." and I wrote a program that seems to work well, but I am confused with my own program. Here is the code attached.

    string remove_duplicates(string &s1)
    {
       int n=s1.size();
       for(int i=n-1; i!=-1; --i)
            for(int j=0; j<i; ++j)
            {
                if(s1[i]==s1[j])
                {
                    int k=i;
                    while(k!=n)
                    {
                        s1[k]=s1[k+1];
                        k++;
                    }
                }
            }
        return s1;
    }
If s1=abcdeafg, the output will be abcdefg by using this code, (and if input is abababab, output will be ab)
But what I thought is that because the length of s1 does not change, the output should be abcdefga since I just move the second 'a' to the end of s1. Could you guys help me explain it?
Thanks in advance, and have a great weekend.

Best Regards,