C语言字符串操作--删除子串

来源:互联网 发布:三层网络ping 请求超时 编辑:程序博客网 时间:2024/04/30 02:36

一,删除字符串中所有子串

#include<string.h>

#include<stdio.h>
void main()
{
 char a[15]="Hi Hillo Hi ab";
 char *p=a;
 char *q;
 char *temp="Hi";
 int i;
 i=strlen(temp);
 
 while((q=strstr(p,"Hi"))!=NULL)
 {
  strcpy(q,q+i);
 }
 printf("p=%s\n",p);

}

结果:p= llo ab



二,删除字符串中第一个子串,把while((q=strstr(p,"Hi"))!=NULL)换成if((q=strstr(p,"Hi"))!=NULL)即可


运行结果:p= Hillo Hi ab



0 0
原创粉丝点击