免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2188 | 回复: 5
打印 上一主题 下一主题

[C] 关于删除字符串,请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-09 19:03 |只看该作者 |倒序浏览
今天无意中看到一面试题,是关于把串str1中的str2串删除.
结果自己写了如下的代码,虽然能正常运行,但是我在网上
看到了一种kmp算法,看了半天没怎么明白,能否请高手用
kmp算法给我实现一下呢?谢谢,呵呵
我写的如下,写的比较乱,见谅.

  1. /*
  2. * Delete the string str2 from string str1.
  3. */


  4. #include <string.h>
  5. #include <stdio.h>

  6. int del_str(char *str1,const char *str2)
  7. {
  8.         const char      *p2 = str2;
  9.         char    *p1 = str1;
  10.         char    *tmp = NULL;
  11.         int     p1_length = strlen(p1);
  12.         int     p2_length = strlen(p2);
  13.         int     same = 0;
  14.         int     i;

  15.         if (p1_length < p2_length) {
  16.                 return -1;
  17.         }

  18.         while (1) {
  19.                 if (*p2 == *p1) {
  20.                         if (same == 0) {
  21.                                 tmp = p1;
  22.                         }
  23.                         same++;
  24.                         if (same == p2_length) {
  25.                                 p1 = tmp;
  26.                                 for (i = 0; *(tmp+i) != '\0' ;i++) {
  27.                                         *(p1++) = *(tmp+same+i);
  28.                                 }
  29.                                 *p1 = '\0';
  30.                                 return same;
  31.                         } else if (same < p2_length) {
  32.                                 p1++;
  33.                                 p2++;
  34.                         }
  35.                 } else {
  36.                         same = 0;
  37.                         p1++;
  38.                         p2 = str2;
  39.                         if (*(p1) == '\0') {
  40.                                 return same;
  41.                         }
  42.                 }
  43.         }
  44. }

  45. int main(int argc,char *argv[])
  46. {
  47.         if (argc < 3) {
  48.                 fprintf(stderr,
  49.                         "Useage com str1 str2\n");
  50.                 return -1;
  51.         }

  52.         int     n;

  53.         if ((n = del_str(argv[1],argv[2])) == 0) {
  54.                 fprintf(stderr,
  55.                         "No matched the str2 in str1\n");
  56.         } else if (n < 0 ) {
  57.                 fprintf(stderr,
  58.                         "The str2's length is longer then str1's length\n");
  59.         } else {
  60.                 printf("The result string is %s\n",argv[1]);
  61.                 fflush(stdin);
  62.         }

  63.         return 0;
  64. }

复制代码

[ 本帖最后由 fydream 于 2008-1-9 19:28 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-01-09 20:12 |只看该作者
KMP算法的源码网上很多啊

论坛徽章:
0
3 [报告]
发表于 2008-01-09 20:20 |只看该作者
原帖由 baohuaihuai 于 2008-1-9 20:12 发表
KMP算法的源码网上很多啊


大多是用C++写的,看不太明白

论坛徽章:
0
4 [报告]
发表于 2008-01-09 22:09 |只看该作者

回复 #3 fydream 的帖子

看了你的代码是比较复杂,为什么不用strstr来完成呢?规定了吗?:)

论坛徽章:
0
5 [报告]
发表于 2008-01-09 22:09 |只看该作者

论坛徽章:
0
6 [报告]
发表于 2008-01-10 08:45 |只看该作者
原帖由 converse 于 2008-1-9 22:09 发表
http://www.cppblog.com/converse/archive/2006/07/05/9447.html

converse的个人blog??
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP