Your requirement is so particular that I doubt there's an already made function to do what you want. It seems like looking for the character at every insertion is the only way to go, but I can think of two approaches:
If your function will be handling relatively short strings, you look for a similar character at every insertion, looking in the string you're building.
For longer strings what you could do is have an array as big as your character set. Let's say you have 50 valid characters, then the size of your array would be 50.
//initialize array to all zeros
for (char c : tableChars)
{
if (array[c] == 0)
{
s += c;
array[c] = 1;
}
}
Note that array[c] is just pseudocode, and you would need to adjust it to use the ASCII character as the index, or build some custom logic around it.