急!关于双循环的问题

来源:百度知道 编辑:UC知道 时间:2024/04/27 20:25:40
有两个字符型数组char(s[],t[])寻找在s[]中t[]的位置如果有则将位置返回,如果没有就返回-1

int find(const char s[],const char t[])/*在s中寻找t,如果找到就返回t在s中的起始位置下标,否则返回-1*/
{
int i,j;
for (i = 0; s[i]; ++i) {
for (j=0; t[j]&&s[i+j]; j++)
if (t[j]!=s[i+j]) break;
if (t[j]==0) return i;
}
return -1;
}

#include<stdio.h>
#include<string.h>
int fun(char s[],char t[])
{//比较字符函数
int i=0,j=0,m=strlen(s),n=strlen(t);//求出字符个数!
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{
if(s[j+i]!=t[j])
break;
if(j==n-1)
return 1;
}//返回值
}

return -1;
}
void main()
{
char s[6]={"abcde"},t[3]={"cl"};//初始字符数组!
int d;
d=fun(s,t);
if(d==1)//判断是否有匹配,也就是t不在s中
printf("yes!");
else
printf("no!");

}