I'm really confused on this part of an assignment for my C++ class. Can anyone help?
Here it is:
Write a program that finds a substring inside a string. Main program asks users to type a string and then a substring. Then, program calls a subfunction int mystrstr(char *str, char *substr). Inside the mystrstr subroutine, program searches substring substr in the string str and returns the starting position of the substring in the string. If the substring is not located in the string, then subroutine return -1. After searching the position, main program prints out the string from the position to the end of string of the string. e.g. string: "C++ program is really fun", substring: "program", Then, your program will print "program is really fun"
I believe there's a function to do that. (At least, if you use std::string as opposed to crappy old char[]. If you can use string, then you can look up the members in the reference section for how to find substrings.) If you must use char[], then you'd have to (here's how I'd do it, at least)
1. check: is the source string big enough to even contain the substring?
2. cycle through the characters of the source string, looking for the starting char of the substring.
3. If you find it, check the char immediately after for the substring's second char, etc.
4. If you find that the entire substring matches, you know the position. Return it.
5. If it doesn't work, keep looking through for the starting character from where you left off.