- /*
- *字符串的朴素匹配
- 通过每一个字母对应着主串
- 进行一次的进行比较,知道
- 其中的一个串的所有字母都匹配成功
- */
- #include <iostream>
- #include <cstdio>
- #include <malloc.h>
- #include <cstring>
- using namespace std;
- int index(char *a, char *b)
- {
- int tarindex = 0;
- while(a[tarindex] != '\0')
- {
- int tarlen = tarindex;
- int patlen;
- for(patlen = 0; b[patlen] != 0; patlen++)
- {
- if(a[tarlen++] != b[patlen])
- {
- break;
- }
- }
- if(b[patlen] == '\0')
- {
- return tarindex;
- }
- tarindex++;
- }
- return -1;
- }
- int main()
- {
- char *a;
- char *b;
- a = (char*)malloc(sizeof(char));
- b = (char*)malloc(sizeof(char));
- gets(a);
- gets(b);
- cout<<"第 "<<index(a, b) + 1<<" 个字母开始匹配!"<<endl;
- return 0;
- }
来源: http://www.phpxs.com/code/1004272/