You don't need a second array and you don't need to move things around all the time either.
Code:
#include <stdio.h>
int main() {
char b[256], c;
int size = 0;
while ((c = getchar()) != '\n') {
if (size == 256) break;
b[size++] = c;
}
int from = 0, to = 0;
while (from < size) {
if (from < size - 2 && b[from] == 'a'
&& b[from+1] == 'a'
&& b[from+2] == 'a') {
b[to++] = '*';
from += 3;
}
else
b[to++] = b[from++];
}
size = to;
for (int i = 0; i < size; i++)
putchar(b[i]);
putchar('\n');
return 0;
}
Or you could do it as you read in the line.
Code:
#include <stdio.h>
int main() {
char b[256], c;
int size = 0, acnt = 0;
while ((c = getchar()) != '\n') {
if (size == 256) break;
if (c == 'a') {
if (++acnt == 3) {
b[size++] = '*';
acnt = 0;
}
}
else {
for ( ; acnt > 0; --acnt) b[size++] = 'a';
b[size++] = c;
}
}
for ( ; acnt > 0; --acnt) b[size++] = 'a';
for (int i = 0; i < size; i++)
putchar(b[i]);
putchar('\n');
return 0;
}