- #include <stdio.h>
- #include <string.h>
- int str_match (const char * , const char *);
- int main (void) {
- int location;
- char target_str[80];
- char patten_str[80];
- gets(target_str);
- gets(patten_str);
- location = str_match(target_str, patten_str);
- printf ("%d\\n", location);
- return 0;
- }
- int str_match (const char * t , const char * p) {
- int t_len = strlen(t);
- int p_len = strlen(p);
- int i, j;
- if (t_len < p_len) {
- return -1;
- }
- for (i=0; i<t_len-p_len; i++) {
- for (j=0; j<p_len; j++) {
- if (p[j] != t[i+j]) {
- break;
- }
- }
- if (j == p_len) {
- return i;
- }
- }
- return -1;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2512201411400.html
来源: http://www.codesnippet.cn/detail/2512201411400.html