- 论坛徽章:
- 0
|
本帖最后由 ddgfff 于 2010-11-27 13:59 编辑
LZ,我针对你的这个需求写了个函数,呵呵~- #include <stdio.h>
- #include <string.h>
- static char *del_some_str (char *str, char *del, char c)
- {
- char *ptr = NULL, *strp = str;
- int len_str = strlen(str);
- int len_del = strlen(del);
- while ( (ptr = strstr(strp, del)) != NULL ) {
- if (*(ptr + len_del) == '\0' && (ptr == strp || *(ptr - 1) == c))
- *(ptr - 1) = '\0';
- else if (*(ptr + len_del) == c && (ptr == strp || *(ptr - 1) == c))
- memmove (ptr, ptr + len_del + 1, (str + len_str) - ptr);
- else
- if (*(ptr + len_del) != '\0')
- strp = ptr + len_del + 1;
- else break;
- }
- return str;
- }
- int main(void)
- {
- char str[] = "192.168.1.1,192.168.1.10,192.168.1.10-192.168.1.30,192.168.2.89,192.168.4.11,192.168.1.1";
- char del[] = "192.168.1.1";
- char *p;
- puts(str);
- p = del_some_str (str, del, ',');
- puts(p);
- return 0;
- }
复制代码 |
|