删除字符1中所有属于字符串2的字符

方法:先建立一个bool hash[256]来记录字符串2中出现的所有字符情况

   建立一个指针p_cur 来指向字符串1的首字符,用这个指针来刷新字符串1的字符

   将字符串1中的字符和hash表来对比,如果这个字符没有在字符串2中出现过,就用*p_cur++=*p_src来刷新字符串1,否则,跳过这个字符。

#include <iostream>
using namespace std;

//删除字符串1中所有属于字符串2的字符
int delete_special_char(char*p_src, char*p_del)
{
    
    if (p_src == NULL || p_del == NULL)
        return 0;
    int len_src = strlen(p_src);
    int len_del = strlen(p_del);
    if (len_src == 0 || len_del == 0)
        return 0;
    
    bool hash[256] = { false};
    for (int i = 0; i < len_del ;i++)
        hash[p_del[i]] = true;

    char *p_cur = p_src;
    while (*p_src!='\0')
    {
        if (hash[*p_src] == false)
            *p_cur++ = *p_src;
        p_src++;
    }
    *p_cur = '\0';
    
}
int main()
{
    char src[50], del[50];
    cout << "输入两个字符串,中间用空格隔开";
    cin >> src >> del;
    delete_special_char(src, del);
    cout << src;
    system("pause");
}

 

posted @ 2018-04-14 08:49  新新苦苦的学习  阅读(1756)  评论(0编辑  收藏  举报