<p>If I have the string <code>.....ZZ..ZZ.....</code> or <code>.Z.1.Z.23Z.4.Z55</code>,</p>

<p>Is there an easy way of shifting all <code>Z</code>characters in the string one space right of the current position?</p>

<p>Some additional test strings are:</p>

<ul>
<li><code>.Z</code></li>
<li><code>Z.</code></li>
<li><code>ZZ.</code></li>
<li><code>.ZZ</code></li>
<li><code>Z</code></li>
<li><code>ZZ</code></li>
<li><code>ZZZ</code></li>
</ul>

<p>I think a few of the higher voted answers to this question (including the currently accepted one) do not work on these tests.</p>

Just iterate through the text and swap characters:
```
```
int main ()
{
    char text[] = "...Z.Z.Z...", temp;
    int text_len = strlen (text), i;
    for (i = text_len - 1; i >= 0; i--)
    {
        if (text[i] == 'Z')
        {
                temp = text[i+1];
                text[i+1] = text[i];
                text[i] = temp;
        }
    }
    printf ("%s\n", text);
    return 0;
}
```
```
Produces:
```
```
[~]$ gcc zshift.c && ./a.out
....Z.Z.Z..
```
```
There's a lot of discussion in the comments about a possible off-by-1 error in the above code. However, simple testing / stepping through is enough to show that this is not the case.
```
```
zshift "Z." -> ".Z"
zshift ".Z" -> "."
zshift "Z" -> ""
```
```
I think the behavior of "dropping" trailing Zs when shifting off the end of the string is sensible. After all, if you shift the bits of an integer, bits that end up outside the bounds of the integer are dropped.
If another behavior is desired -- for example, shifting only within the string -- the change to the algorithm is minimal:
```
```
temp = text[i+1];
if (temp == 0) continue;
text[i+1] = text[i];
text[i] = temp;
```
```