• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
乐碎碎
程序媛想的事儿
博客园    首页    新随笔    联系   管理    订阅  订阅
删除一个字符串中的子串
 1 // 删除一个字符串中的子串
 2 
 3 #include "stdafx.h"
 4 #include <string.h>
 5 
 6 int delete_substr(char *str1,char *str2)
 7 {
 8     int pos=0,k=0,staic=0,n;  
 9     int str1_len=strlen(str1);
10     int str2_len=strlen(str2);
11     for(int i=0;i<str1_len;i++)
12     {
13         for(int j=0;j<str2_len;j++)
14         {
15             if (str1[i]==str2[j])
16             {
17                 staic=1;
18                 k=1;
19                 pos=i;
20                 while((str1[i+k]==str2[j+k])&&(str1[i+k]!='\0')&&(str2[j+k]!='\0'))
21                     {
22                         k++;
23                     }
24                 break;
25             }
26         }
27         if (staic==1)
28             break;
29     }
30     if(k==str2_len) //找到子串
31     {
32         n=pos+str2_len;  //pos是找到子串在str1中的位置
33         strcpy(str1+pos,str1+n);
34         return 0;
35     }
36     else 
37         return -1;
38 }
39 
40 void main()
41 {
42     int flag;
43     char s1[]="abcdddef";
44     char s2[]="bcdd";
45     printf("s1=%s\n",s1);
46     printf("s2=%s\n",s2);
47     flag=delete_substr(s1,s2);
48     if (flag==0)
49         printf("Succes! \nAfter delete :s1=%s\n",s1);
50     else
51         printf("Failed!The substr is not in string.\n");
52 
53 }

 

 

posted on 2012-10-04 18:35  xingle0917  阅读(438)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3