晕!这就是最简单的了,你把头文件string.h包含起来调用strstr()函数就完成了。
string.h的库函数strstr()帮你完成此项功能,strstr()的功能就是在一个字符串中查找另一个字符串。下面是测试函数及strstr()函数。
#include
#include
#include
int main(void) /*测试strstr()*/
{
char *A="abcdefghi",*B,*C;
B = (char*)malloc(10);
memset(B, 0, sizeof(B));
C = (char*)malloc(10);
memset(C, 0, sizeof(C));
B[0]='f';B[1]='g';B[2]='h';
C = strstr(A,B);
printf("A=\"%s\"\n",A);
printf("B=\"%s\"\n",B);
printf("C=\"%s\"\n",C);
return 0;
}
#if 0
/*strstr()函数的返回值为s中匹配字窜的地址*/
char * strstr
(
const char * s, /* string to search */
const char * find /* substring to look for */
)
{
char *t1;
char *t2;
char c;
char c2;
if ((c = *find++) == 0) /* an empty string */
return ((char *)(s));
for (;;)
{
while (((c2 = *s++) != 0) && (c2 != c))
;
if (c2 == 0)
return (NULL);
t1 = (char *)(s);
t2 = (char *)(find);
while (((c2 = *t2++) != 0) && (*t1++ == c2))
;
if (c2 == 0)
return ((char *)(s - 1));
}
}
#endif