大神帮我看看哪里错了吧?明天就要交作业了.题目是这样的:编制函数,在字符串中查找与另一个字符串相等的字符串,函数返回值为该字符串的地址或NULL.我的思路:把相同的字符存在另一个

来源:学生作业学帮网 编辑:学帮网 时间:2024/04/29 02:36:51

大神帮我看看哪里错了吧?明天就要交作业了.
题目是这样的:编制函数,在字符串中查找与另一个字符串相等的字符串,函数返回值为该字符串的地址或NULL.
我的思路:把相同的字符存在另一个数组c中,然后返回c.但是我不知道哪里错了.求指导,指针的知识没跟得上.
# include
# include
# include
# include
char * same(int * a,int * b,int x,int y)
{
char *c=(char*)malloc(sizeof(char)*10);
int i,j,k = 0,n;
for (i = 0;i

思路错了.下面是正确的——
char *same(char *a,char *b){
int i,j;
char *p;
for(i=0;a[i];i++){
for(p=a+i,j=0;b[j];j++)
if(a[i+j]!=b[j]) break;
if(!b[j]) return p;
}
return NULL;
}
void main(void){
char a[100],b[100];
printf ("请输入数组a的元素:\n");
gets(a);
printf ("请输入数组b的元素:\n");
gets(b);
printf ("%p\n",same(a,b));
}