I have a working piece of C code that completely removes every second char from a character array, making the original array half the size (half+1 if the size was odd)
..but I cannot figure out how it works.
void del_str(char string[]) {
int i,j;
for(i=0, j=0; string[i]!=0; i++) {
if(i%2==0) {
string[j++]=string[i];
}
}
string[j]=0;
}
//
example input: 'abcdefgh'
output from that: 'aceg'
what I thought the output would be: 'aacceegg'
The line I don't understand is
string[j++]=string[i];
I can write code that omits every second char, so the output would be:
'a c e g '
but I can't wrap my head around this.
Alternatively, how would you write a program that completely deletes every n-th char and their space in the original array? (producing the same output as the above code)