字串删除——利用strstr()函数

描述

给定两个字符串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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章