题目:写一个C程序处理带有删除符的字符串(例如删除符是*)。比如"abc***def**g",处理后是"dg",“a**b*cdef*g”处理后是"cdeg", "abcd**g**"处理后是“a”,其它的都被删除了。函数声明为 int process(char *s); s为可以更改的内存,请在原来内存进行操作不要分配新的内存。
#include <stdio.h>
#include <string.h>
int offset(char *ab, int slen)
{
int len = 0;
int i, j;
int ret_len;
for (i = 0; i < slen; i++)
{
if (ab[i] == '*') // 查找星号(*)前有多少个字符
{
for (len=0; len< (slen - i); len++) // 查找有多少个星号(*)
{
if (ab[i+len] != '*')
{
break;
}
}
if (i > len) {
ret_len = slen - len*2;
for (;i + len <= slen; i++)
{
ab[i-len] = ab[i+len];
}
}
if (i == len) {
for(i = 0; len*2 + i <= slen; i++)
{
ab[i] = ab[len*2+i];
i++;
}
ret_len = slen - 2*len;
}
if (i < len) {
for (j = 0;i + len + j <= slen; j++)
{
ab[j] = ab[i + len + j];
}
ret_len = slen - (i + len);
}
return ret_len;
}
}
return -1;
}
int process(char *ab)
{
int i;
int new_len = 0;
int byd_len;
int all_len = strlen(ab);
new_len = all_len;
while (new_len > 0)
{
byd_len = new_len;
new_len = offset(ab, new_len);
}
for (i = byd_len; i < all_len; i++)
{
ab[i] = '\0';
}
printf("%s\n", ab);
return 0;
}
int main(int argc, char *argv[])
{
char ab[100] = {0};
process(argv[1]);
return 0;
} /*add by tanya.h@aliyun.com 2018.07.11*/