刪除字符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");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM