实现子串定位int FindSubStr(const char *MainStr, const char *SubStr)
#include <cstdio>
#include <deque>
#include <algorithm>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
int FindSubStr(const char* MainStr, const char* SubStr)
{
assert(MainStr != NULL&&SubStr != NULL); //检查
const char *p;
const char *q;
const char *s= MainStr;
while (*MainStr)
{
p = MainStr;
q = SubStr;
while (*p && *q && *p++ == *q++); //对子串进行全部的检查
if (!*q)
{
return MainStr - s + 1;
}
MainStr++;
}
return -1;
}
int main()
{
char *a = "33355555";
char*b = "55555";
cout << FindSubStr(a, b) << endl;
system("pause");
return 0;
}
本文介绍了一个简单的子串定位算法的实现过程。该算法通过遍历主字符串并逐字符比较子字符串来查找子字符串的位置。文章包含完整的源代码示例,并提供了一个测试案例。
802

被折叠的 条评论
为什么被折叠?



