I need to write a programm where two strings are entered through the keyboard and the 1st string is searched for the 2nd and then the point where it first appeared is given out.
#include <stdio.h>
#include <string.h>
int main() {
char in1[10000];
char in2[10000];
fgets(in1, sizeof(in1), stdin);
fgets(in2, sizeof(in2), stdin);
printf("%d", strstr(in1, in2) - in1+1);
return 0;
}
I did 3 tests
in1=11121
in2=121
result=3 correct
in1=11121121
in2=121
result=6 wrong
in1=11121211
in2=121
result=-2348863 obviously wrong
in1=111211211211
in2=121
result=-2348879
I don't know why it gives out the 2nd time the sequence occurs if it occurs twice and a big negative number if it occurs thrice.
What did I do wrong?
-in1+1, whenstrstrreturns null after not finding anything. Please post a version of your program that actually produces the outputs you show above.