剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符

// 从第一个字符串中删除第二个字符串中出现过的所有字符

#include <stdio.h>

char* remove_second_from_first( char *first, char *second )
{
    if( first == NULL || second == NULL )
    {
        printf("first or/and second should not be NULL\n");
        return NULL;
    }

    char flag[256]={0};
    char *p, *q;

    p = second;
    while( *p != '\0' )
        flag[*p++] = 1;

    p = q = first;
    while( *q != '\0' )
    {
        if( flag[*q] == 1 )
        {
            q++;
            continue;
        }
        *p++ = *q++;
    }
    *p = '\0';

    return first;
}

int main(void)
{
    char s1[101], s2[101];
    scanf("%s",s1);
    scanf("%s",s2);
    char *result = remove_second_from_first(s1,s2);
    printf("%s\n",result);

    return 0;
}
posted on 2014-07-24 01:18  DayByDay  阅读(686)  评论(0编辑  收藏  举报