C实现substr()函数

C语言标准库中没有  int substr(char *s1, char *s2)/*字符串匹配,成功返回所在位置,不成功返回-1*/  函数,下面是利用已有的库函数自己实现的substr函数。

代码如下:

 1 #include <stdio.h>
 2 
 3 #include <string.h>
 4 
 5 int substr(char *s1, char *s2)
 6 {
 7     char *s3 = strstr(s1,s2);
 8     if(s3 == NULL)
 9         return -1;
10     return strlen(s1)-strlen(s3);
11 }
12 /*test*/
13 int main()
14 {
15     char *s1 = "4567890";
16     char *s2 = "78900";
17     printf("%d\n",substr(s1,s2));
18     return 0;
19 }

 

posted @ 2015-03-02 14:23  永久指针  阅读(14419)  评论(0编辑  收藏  举报