Implement the following functions. Each function deals with null terminated C-Style strings. You can assume that any char array passed into the functions will contain null terminated data. Place all of the functions in a single file and then create a main() function that tests the functions thoroughly.
Note: You may not use any c-string functions other than strlen().
I am having trouble with the fourth function. The desired behavior is: This function returns the index in string s where the substring can first be found. For example if s is "Skyscraper" and substring is "ysc" the function would return 2. It should return -1 if the substring does not appear in the string.
prototype:
Note: You may not use any c-string functions other than strlen().
Use substring[i] instead of *(substring+i). They mean the same thing but the former is more common and thus more readable.
I don't understand how your algorithm is supposed to work. Basically you want to go through str from left to right checking if the substring matches at each position.
1 2 3 4 5 6
size_t strLen = strlen(str);
size_t substrLen = strlen(substr);
for (i = 0; i < strLen-substrLen; ++i) {
// if the first substrLen chars of str starting from position i match substring then return i
}
return -1
You can't use C string functions, but take a look at memcmp(). This function compares blocks of memory. You can use it to the code inside the loop.