你的写法是判断两个指针地址是否相等,而不是指针指向的内容相同。
p和"delete"的地址原本就不相等,一个指向了栈上分配了10个字节的空间,空间的内容是"delete",另一个指向了常量区的"delete"。
请采纳,谢谢
晕!这就是最简单的了,你把头文件
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