人生境界:明智、中庸、诚信、谦逊
分类: C/C++
2013-02-22 16:45:02
第一种实现:
char *strstr( const char *s1, const char *s2 ) { int len2; if ( !(len2 = strlen(s2)) ) return (char *)s1; for ( ; *s1; ++s1 ) { if ( *s1 == *s2 && strncmp( s1, s2, len2 )==0 ) return (char *)s1; } return NULL; }第二种实现:
char * strstr(const char * s1,const char * s2) { int l1, l2; l2 = strlen(s2); if (!l2) return (char *) s1; l1 = strlen(s1); while (l1 >= l2) { l1--; if (!memcmp(s1,s2,l2)) return (char *) s1; s1++; } return NULL; }