CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters

来源:互联网 发布:up to date数据库介绍 编辑:程序博客网 时间:2024/04/30 13:49

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;}


0 0
原创粉丝点击