7 46 刪除字串中的子串 find

2021-10-02 14:21:38 字數 1300 閱讀 8234

輸入2個字串s1和s2,要求刪除字串s1**現的所有子串s2,即結果字串中不能包含s2。

輸入格式:

輸入在2行中分別給出不超過80個字元長度的、以回車結束的2個非空字串,對應s1和s2。

輸出格式:

在一行中輸出刪除字串s1**現的所有子串s2後的結果字串。

tomcat is a male ccatat

cat

tom is a male
#include

#include

using

namespace std;

intmain()

cout

}

find()

說明:如果查詢成功則輸出查詢到的第乙個位置,否則返回-1;

查詢第一次出現的目標字串:

#include

#include

using

namespace std;

intmain()

//輸出:3

查詢從指定位置開始的第一次出現的目標字串:

#include

#include

using

namespace std;

intmain()

//輸出:3

find_first_of()

查詢子串中的某個字元最先出現的位置

find_first_of()不是全匹配,而find()是全匹配

#include

#include

using

namespace std;

intmain()

//輸出:1

也可以約定初始查詢的位置:s1.find_first_of(s2, 2) ;

#include

#include

using

namespace std;

intmain()

//輸出2

find_last_of()

這個函式與find_first_of()功能差不多,只不過find_first_of()是從字串的前面往後面搜尋,而find_last_of()是從字串的後面往前面搜尋。

rfind()

反向查詢字串,即找到最後乙個與子串匹配的位置

find_first_not_of()

找到第乙個不與子串匹配的位置

刪除字串中的子串

請編寫乙個函式,刪除乙個字串的一部分。函式原型如下 int del substr char str,char const substr 函式首先應判斷substr是否出現在str中。如果它並未出現,函式就返回0 如果出現,函式應該把str中位於該子串後面的所有字元複製到該子串的位置,從而刪除這個子串...

刪除字串中的子串

輸入2個字串s1和s2,要求刪除字串s1 現的所有子串s2,即結果字串中不能包含s2。輸入格式 輸入在2行中分別給出不超過80個字元長度的 以回車結束的2個非空字串,對應s1和s2。輸出格式 在一行中輸出刪除字串s1 現的所有子串s2後的結果字串。輸入樣例 tomcat is a male ccat...

刪除字串中的子串

輸入2個字串s1和s2,要求刪除字串s1 現的所有子串s2,即結果字串中不能包含s2。輸入格式 輸入在2行中分別給出不超過80個字元長度的 以回車結束的2個非空字串,對應s1和s2。輸出格式 在一行中輸出刪除字串s1 現的所有子串s2後的結果字串。輸入樣例 tomcat is a male ccat...