I've encountered a couple of problems with the program that i'm writing now.
- strstr outputs my substring only if it's at the end of my string
- it also outputs some trash characters after that
- I've had problems with "const char *haystack" and then adding input to it, so i did it with fgets and getchar loop
- somewhere along the way it worked with a substring that was not only at the end, but then i outputted substring and the rest of the string ater that
here is my main:
int main() {
char haystack[250],
needle[20];
int currentCharacter,
i=0;
fgets(needle,sizeof(needle),stdin); //getting my substring here (needle)
while((currentCharacter=getchar())!=EOF) //getting my string here (haystack)
{
haystack[i]=currentCharacter;
i++;
}
wordInString(haystack,needle);
return(0);
}
and my function:
int wordInString(const char *str, const char * wd)
{
char *ret;
ret = strstr(str,wd);
printf("The substring is: %s\n", ret);
return 0;
}