squeeze滤除特定字符或单词

#include 

#define MAXLEN 100

void squeeze(char s[], char c);
void squeeze_word(char s[], char c[]);

int main(int argc, char *argv[]) {
    char s[] = "hello world!";
    char word[] = "hello";

    squeeze_word(s, word);

    printf("String: %s\n", s);

    return 0;
}

void squeeze(char s[], char c) {
    char *p = s;
    while(*p !='\0') {
        if (*p != c) {
            *s++ = *p++;
        } else {
            ++p;
        }
    }
    *s = '\0';
    return;
}

void squeeze_word(char s[], char c[]) {
    char *p = s;

    while (*p != '\0') {
        if (*p == *c) {
            char *m = p;
            char *n = c;
            while ((*m == *n) != '\0') {
                ++m;
                ++n;
            }
            int len = 0;
            if (*n == '\0') {
                len = n - c;
                p = p + len;
            }
        }
        *s++ = *p++;
    }
    *s = '\0';
    return;
}

你可能感兴趣的:(C程序设计语言)