描述
給定兩個字符串s和t,若s是t的子串,將t中的子串s刪除,若存在多個子串,則全部刪除;若s不是t的子串,對字符串t不做處理。字符串s和t長度不超過1000。
輸入
第一行,字符串t,文本長度\le≤1000。
第二行,字符串s,文本長度\le≤1000。
輸出
處理後的字符串t
輸入樣例 1
Hello World!
Hello
輸出樣例 1
World!
輸入樣例 2
Hello World!
Ho
輸出樣例 2
Hello World!
輸入樣例 3
No pain, no gain
ain
輸出樣例 3
No p, no g
strstr()函數,返回字符串中首次出現字串的地址,無則返回NULL。(那麼我們用指針接收)
example:
strstr(s,t);//表示返回s串中t串出現的首次地址。
#include <stdio.h>
#include <string.h>
int main()
{
char* p,*a;
char s[1001],t[1001];
gets(s);
gets(t);
a=s;
do
{
p=strstr(a,t);
if(p!=0){
for( ;a<p;a++)
printf("%c",*a);
a=a+strlen(t);
/*實際上我們是將含有字串的位置跳過了,
不是真正意義上的刪除*/
}
}while(p!=0);
puts(a);
return 0;
}