I am trying to make a function which finds the position of a substring in a char pointer and I have a problem: it doesn't return the right position but the "word" string contains bad characters. For example, for 'strfnd("Hello everybody!","everybody")' it returns 0. I am trying as much I can to avoid the string class. Here is my code
char word[length(substr)]; is not ISO standard C++. Static array sizes must be determined at compile time. Instead you could try dynamically allocating memory. http://www.cplusplus.com/doc/tutorial/dynamic/
Please tell me why doesn't it work without "cout".
Probably because the compiler generated a section of code that somehow "accidentally" had memory available for char word[]
Pro Tip: You do not need to create a new array in this function. Because you could just increment the haystack pointer.
By substr() I assume you mean strstr() which does indeed return the position of the found substring. If you need an integer index all you need to do is a bit of pointer arithmetic foundPointer - originalPointer https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html