刪除特定字元

2021-06-25 10:23:54 字數 1268 閱讀 1683

題目大意:編寫乙個高效率的演算法來刪除字串中的給定字元。比如,「this is a student!」是源字串,「aeiou」是刪除字串,則結果為「ths s   stdnt!」.

一開始想到新建兩個陣列,乙個用下標表示刪除字串中出現的字元,乙個用來儲存經過處理的字串,結果輸出第二個陣列,**如下:

#include #include using namespace std;

static char remove_list[256];

void deletechar(char *source,char *remove)

for(i = 0;i < m;i++)

int index = 0;

for(i = 0;i < n;i++)

}for(i = 0;i < index;i++)

{cout<

#include#includeusing namespace std;

void deletechars(char *source, char *remove)

{ if(source==null || remove==null)

return;

//remove_list標記字元是否在字串second中出現...

bool remove_list[256];

memset(remove_list,0,sizeof(remove_list));

size_t len1 = strlen(source);

size_t len2 = strlen(remove);

//掃瞄字串remove,在remove_list中標記出現的字元

for(int i=0; i另外要注意的是,如果我們將char source = "this is a student!"改為char *source = "this is a student!",則執行時會錯誤。

因為char *source = "this is a student!"中,source指標指向的是乙個常量記憶體區,通過傳址方式改變其中的值當然會報錯。而char source = "this is a student!"中source是乙個一維陣列,且"this is a student!"存在的區域也不是在常量記憶體區,所以可以改變其值。

舉例說明其他情況:

*pp = "abc";

p = "abc";

*pp指向的是字串中的第乙個字元。

cout<<*(p+1);  返回第二個字串:b

cout<<&p[1];    返回從第二個字元開始的字串:bc

SQL刪除特定字元

請教sql刪除特定字元 sql語句為 update table name set field name replace field name from str to str 說明 table name 表的名字,field name 欄位名,from str 需要替換的字串,to str 替換成的字...

面試經典(2) 刪除特定字元

題目 輸入兩個字串,從第一字串中刪除第二個字串中所有的字元。例如,輸入 they are students.和 aeiou 則刪除之後的第乙個字串變成 thy r stdnts.分析 我們考慮如何在字串中刪除乙個字元。由於字串的記憶體分配方式是連續分配的。我們從字串當中刪除乙個字元,需要把後面所有的...

python 刪除特定字元所在行

查詢檔案中含有特殊字串的行 usr bin python coding utf 8 import re file1 open test.txt r istxt re.compile r if.re.i for line in file1.readlines line line.strip ifstr...