C语言问题
int find(char*s ,char*t){
char*p=s;
char*q=t;
int indx=0,i=0,le = strlen(q);
while(*p!=*q){
p++;
indx++;
}
while(*q!='\0' && *p++==*q++) i++;
if(i<le) indx=0;
return indx;
}
我按自己的想法写的是 int型的函数
可以实现,但题目中是要使用返回char*的函数,还要用返回值-s得到位置,不知所措了,想了半天没想出来,求大神帮帮忙看看这道题该怎么解。
百度一下 指针函数,你这代码写得挺6的呀,咋不把基础看完 char* search(char *s, char *t)
{
if(s1==NULL)
return NULL;
/*
如果s2为NULL,则返回s1
*/
if(s2==NULL)
return (char*)s1;
while(*s1)
{
/*
保存每一次开始的位置,方便返回;
*/
char *ps1 = (char*)s1;
char *ps2 = (char*)s2;
while(*ps1&&*ps2&&(*ps1==*ps2))
{
ps1++;
ps2++;
}
if(*ps2=='\0')
return (char*)s1;
if(*ps1!='\0')//检测s2有没有完整的出现在s1中;
s1++;
}
return NULL;
}
页:
[1]