算法练习Mystrstr

草稿

hello wolrd
 
hxl
 
if(*pFirst == *pFindFirst)
    pFirst ++;
    pFindFirst ++;
 
    if(*pFirst == *pFindFirst)
        pFirst ++;
        pFindFirst ++;
 
for(int i ; i < strlen(hello wolrd) ; i++)
{
 
    if(*pFindFirst == '\0')
        return 0;
 
    if(*pFirst == *pFindFirst)
    {
        pFirst++;
        pFindFirst++;
        count ++;
        if(count == strlen(hel))
            return pFirst - count;
        continue;
    }else
    {
        count = 0;
        pFindFirst = findstr;
        pFirst++;
    }
 
}
 
return 0;


于是有了下面代码


#include <stdio.h>
#include <Windows.h>
 
size_t mystrlen(const char* str)
{
    return (NULL == str || *str =='\0') ? 0 : mystrlen(str+1) + 1; 
}
 
 
char* mystrstr(const char* Srcstr, const char* findstr)
{
    if(Srcstr == NULL || findstr == NULL || *Srcstr == '\0' ||*findstr == '\0')
        return NULL;
 
    const char *pFindFirst = findstr;
    const char* pFirst = Srcstr; 
    int count = 0;
    int findstrlen = mystrlen(findstr);
    int srclen = mystrlen(Srcstr);
 
    for(int i = 0 ; i < srclen ; i++)
    {
        if(*pFindFirst == '\0')
            return NULL;
 
        if(*pFirst == *pFindFirst)
        {
            pFirst++;
            pFindFirst++;
            count ++;
            if(count == findstrlen)
                return (char*)(pFirst - count);
            continue;
        }else
        {
            count = 0;
            pFindFirst = findstr;
            pFirst++;
        }
 
    }
 
    return NULL;
}
 
 
 
void main()
{
    EX:
    char *str = (char*)malloc(250);
    char *findstr = (char*)malloc(250);
 
    memset(str,'\0',250);
    memset(findstr,'\0',250);
 
    printf("input src string :");
    gets(str);
    printf("\ninput find string :");
    gets(findstr);
    printf("\n");
 
    char* pRetstr = mystrstr(str, findstr);
 
    if(pRetstr != NULL)
        printf("find string is :%s\n",pRetstr);
    else
        printf("no find!\n");
 
    printf("ExitProcess ? Y # N\n");
    memset(str,'\0',250);
    gets(str);
    if(*str == 'n' || *str == 'N')
        goto EX;
    else
    {
        free(str);
        free(findstr);
        ExitProcess(0);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值