Chinaunix首页 | 论坛 | 博客
  • 博客访问: 829287
  • 博文数量: 125
  • 博客积分: 4066
  • 博客等级: 上校
  • 技术积分: 1401
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-03 18:58
文章分类

全部博文(125)

文章存档

2014年(1)

2013年(1)

2012年(2)

2011年(29)

2010年(92)

我的朋友

分类: LINUX

2010-11-11 15:22:02

#include <stdio.h>
#include <string.h>
#include <assert.h>

char * mystrstr(char * str1, char * str2)
{
        assert(str1 && str2);

        char * main = str1, *sub = str2;
        int len1,len2;

        len2 = strlen(sub);
        if(len2 == 0)
                return NULL;

        len1 = strlen(main);

        if(len2 > len1) {
                return NULL;
        }

        while(len1 > 0) {
                len1--;
                if(len2 > len1)
                        return NULL;
                while(*main++ == *sub++) {
                        if(*sub == '\0')
                                break;
                }

                if(*sub == '\0') {
                        if(strlen(str1) < len2) {
                                return str1;
                        } else {
                                return (str1+len2);
                        }
                } else {
                        sub = str2;
                        main = ++ str1;
                }
        }
        return NULL;

}


int main() {
        char * main = "abiababdaaaefghiabgklzhaixishanabxxxx";
        char * sub = "zhaixishan";
        int count = 0;
       char * find = mystrstr(main,sub);

        while(find) {
                count ++;

                if(find) {
                        // printf("\n %s\n",find);

                }else {
                        printf("\ndonot find !!!\n");
                }
                find = mystrstr(find,sub);
        }
        printf("there is %d sub string in main string\n",count);
}


运行结果:
zhaixishan@zhaixishan-desktop:~/Desktop$ ./a.out
there is 1 count sub string in main string
阅读(1125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~