Reputation:
The code says at many places "invalid indirection".Please help.
int main()
{
char *s1,*s2,*position;
printf("Enter string:\n");
gets(s1);
printf("Enter word to find:\n");
gets(s2);
*position=ststr(*s1,*s1);
if(*position)
printf("word is found at %c loc\n",*position);
else
printf("word not found");
getch();
return 0;
}
char *strstr(char *s1,char *s2)
{
int flag=1;
char i,j;
for(i=0; ;i++)
{
if(*s1[i]==*s2[0])
for(j=i;*s2;j++)
{
if(*s1[j]!=*s2[j])
flag=0;
}
}
if(flag)
return i;
else
return 0;
}
Upvotes: 2
Views: 40970
Reputation: 21
#include "stdio.h"
char *strstr(char *str, char *substr)
{
int len = strlen(substr);
char *ref = substr;
while(*str && *ref)
{
if (*str++ == *ref)
{
ref++;
}
if(!*ref)
{
return (str - len);
}
if (len == (ref - substr))
{
ref = substr;
}
}
return NULL;
}
int main(int argc, char *argv[])
{
printf("%s \n", strstr("TEST IS NOT DONE", "IS NOT"));
}
Upvotes: 2
Reputation: 41
#include <stdio.h>
char* my_strstr(char *s2, char *s1)
{
int i, j;
int flag = 0;
if ((s2 == NULL || s1 == NULL)) return NULL;
for( i = 0; s2[i] != '\0'; i++)
{
if (s2[i] == s1[0])
{
for (j = i; ; j++)
{
if (s1[j-i] == '\0'){ flag = 1; break;}
if (s2[j] == s1[j-i]) continue;
else break;
}
}
if (flag == 1) break;
}
if (flag) return (s2+i);
else return NULL;
}
int main()
{
char s2[] = "This is the statement";
char s1[] = "the";
char *temp;
temp = my_strstr(s2,s1);
printf("%s\n",temp);
return 0;
}
Upvotes: 1
Reputation: 18675
if(*s1[i]==*s2[0])
is such an example where my gcc complains:
error: invalid type argument of ‘unary *’ (have ‘int’)
if s1
is a pointer to char
, s1[i]
is a char. So you can't dereference
it any more (with the *
), i.e. s1[i]
does not point to anything any more.
Try
if(s1[i]==s2[0])
instead.
You should also change the return value of strstr
: you return an integer where you declare to return a pointer to a character. So try returning s1+i
instead.
This here:
for(j=i;*s2;j++)
probably does not what you want. You're not advancing the pointer s2
anywhere in the loop, in fact you're just testing whether s2[0]
(which is the same as *s2
) is zero for each iteration. If s2
isn't the empty string, this loop will never terminate.
Upvotes: 2
Reputation: 6178
One of the problems I'm noticing is whenever you do *s1[j]
. The asterisk is dereferencing the array, and so is the []
notation.
s[i]
really means *(s + i)
, so you don't have to dereference it again. The way you have it would read **(s + i)
, and since it's a single pointer you can't do that.
Upvotes: 2
Reputation: 123468
First, s1
and s2
in main have not been initialized to point anywhere meaningful. Either declare them as static arrays, or allocate memory to them at runtime using malloc()
or calloc()
:
#define SIZE 20 // or some number big enough to hold your input
...
char s1[SIZE], s2[SIZE], *position; // s1 and s2 declared statically
Second, NEVER NEVER NEVER NEVER NEVER use gets()
; it will introduce a point of failure in your program. Use fgets()
instead:
if (fgets(s1, sizeof s1, stdin) != NULL)
// process s1
else
// check for EOF or error on read
EDIT
And like everyone else has pointed out, your comparison in the strstr()
function needs to be either
*s1 == *s2
or
s1[i] == s2[i]
but first you need to deal with allocating your buffers in main properly.
Upvotes: 7
Reputation: 103505
if(*s1[j]!=*s2[j])
*s1
means "the character where s1 is pointing".s1[j]
means "*(s1+j)
" or "the character j positions after where s1 is pointing"You have to use one or the other; not both.
Upvotes: 1