码迷,mamicode.com
首页 > 其他好文 > 详细

求字符串子串的位置

时间:2014-09-16 23:26:51      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   div   sp   问题   log   

经常遇到处理子串问题,最关键求子串的位置。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int getLength(char *a)
 4 {
 5     int i=0;
 6     int j=0;
 7     while(*(a+j)!=\0){
 8         i++;
 9         j++;
10     }
11     return i;
12 
13 }
14 
15 int subString(char*a,char*b)
16 {
17     int index=-1;
18     int i=0;
19     int j=0;
20     int la=getLength(b);
21     while(*(a+i)!=\0)
22     {
23 
24         if(*(a+i)==*(b+j))
25         {
26             j++;
27             if(j==la)
28             {
29                 return i-la+1;
30             }
31         }
32         else
33         {
34             j=0;
35         }
36         i++;
37 
38     }
39 
40     return index;
41 }
42 
43 int main()
44 {
45 
46     char*a="123456";
47     char*b="345";
48     printf("%d \n",subString(a,b));
49     return 0;
50 }

 

求字符串子串的位置

标签:style   blog   color   io   ar   div   sp   问题   log   

原文地址:http://www.cnblogs.com/forsta/p/3975990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!