Eliminate all ‘b’ and ‘ac’ in an array of characters, you have to replace them in-place, and you are only allowed to iterate over
the char array once.
Examples:
abc -> ac
ac->‘‘
rbact->rt
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The key point is in-place and iteration once.
int eliminate( char* p) { int deleted = 0; if (! p ) return deleted; while (*p){ if (*p == ‘b‘) deleted++; else if ( ( *p == ‘a‘ ) && ( *(p+1) == ‘c‘)){ deleted += 2; p++; } else if ( deleted > 0 ) *(p-deleted) = *p; p++; } *(p-deleted) = ‘\0‘; return deleted; }
CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters