【C】笔试题:从字符串中删除子字符串
一道面试题:从字符串A中删除所有的字符串B,返回结果。
写了下,代码如下:
#include <stdio.h> #include <stdlib.h> char *filter(char *str, char *word) { char *p, *q; char *src, *dst; dst = src = str; while(*src != '\0') { p = src; q = word; while(*p == *q && *q != '\0') { p++; q++; } if (*q == '\0') { src = p; } else { *dst++ = *src++; } } *dst = '\0'; return str; } int main(int argc, char *argv[]) { char str[] = "The food is good!"; char word[] = "oo"; printf("%s", filter(str, word)); return 0; }
标签: C
还没有人抢沙发呢~