C语言字串查找作用是什么,C语言版字符串查找函数,字符串中查找子串

操作系统: Windows10 64位

运行环境: Visual Studio 10

依赖的头文件:

#include

#include

/***************************************************************

/*函 数 名:FindSubstring

/*函数功能:C语言版,在字符串中查找子串

/*参 数:

/* strSource:待查找的源字符串

/* strSub: 要查找的子串

/*返 回 值:

/* 返回 0,表示查找成功

/* 返回-1,表示查找失败

/*

/*作 者:X攻城狮

/*日 期:2015年11月4日

/***************************************************************/

int FindSubstring (const char *strSource, const char *strSub)

{

unsigned int uLen = strlen(strSource);

if (uLen == 0)

{

return -1;

}

char *str1 = (char *)malloc(uLen+1);

memset(str1, 0, uLen+1);

strcpy(str1, strSource);

uLen = strlen(strSub);

if (uLen == 0)

{

free(str1);

return -1;

}

char *str2 = (char *)malloc(uLen+1);

memset(str2, 0, uLen+1);

strcpy(str2, strSub);

unsigned int i = 0, j = 0;

for(i=0; i<=strlen(strSource); i++)

{

if (str1[i] == str2[j])

{

j++;

}

else

{

if (j == uLen)

{

break;

}

else

{

j = 0;

}

}

}

free(str1);

free(str2);

if (j == uLen)

{

return 0;

}

else

{

return -1;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值