标签: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