孔庄:急求C++高手解题

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 02:33:15
三、请编写一个函数int find(char s[],char t[]),该函数在字符串s中查找字符串t,如果找到,则返回字符串t在字符串s中的位置(整数值);否则返回-1。注意:用数组方式及两重循环来实现该函数。(10分)
请勿修改主函数main和其他函数中的任何内容,仅在函数find的花括号中填写若干语句。
??#include "stdio.h"
??int find(char s[],char t[]) ;
??const int M =256;
??int main()
??{ char source[M], rarget[M];
?? cout <<"Please input a string for searching:\n" ;
?? cin.getline(source , M);
?? cout<<"Please input a string you want to find:\n";
?? cin.getline(target, M);
?? int intpos = find(source,target);
?? if (intpos>=0 )
?? cout<<"Finding it.The target string is at index"
?? <<intpos<<endl ;
?? else
?? cout<<"Not finding it.\n";
?? return 0;
?? }
??int find(char s[],char t[])
??{
……
?? }

int i,j,judg=1,len;
len=strlen(t);
for(i=0;*s;++i)
{
judg=1;
if(*(s++)==*t)
{
for(j=0;j<len;++j)
if(*(s+j)!=*(t+j))
{
judg=-1;
break;
}
if(judg!=-1)
{
judg=j;
break;
}
}
}
return judg;