C语言版字符串查找函数,字符串中查找子串

来源:互联网 发布:网络电视如何看央视 编辑:程序博客网 时间:2024/05/21 14:54

操作系统: Windows10 64位

运行环境: Visual Studio 10


依赖的头文件:

#include <string.h>#include <stdlib.h>


/***************************************************************/*函 数 名: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;}}


1 0
原创粉丝点击