Create a function called findWord that will return the index of the first charac
ID: 3866632 • Letter: C
Question
Create a function called findWord that will return the index of the first character of a specified word in a CString. Your function should take as parameters the CString to search through and a CString that represents the word to search for. The function will return the index of the first character of the string if it is found. The function should return -1 if the word is not in the string. NOTE: You must use pointers to find the word. This means the use of array notation is not allowed You are not allowed to use function like strstr or strchr to solve this problem. You are to write this algorithm yourself using pointers. To simplify the algorithm this is a case sensitive search. This means upper and lower case characters should be treated as different characters.
Explanation / Answer
char *findword(char *string, char *substr) { for (;;) { if (*string == *substr) { for (char *p1 = string, *p2 = substring;;) { if (!*p2) return string; if (*p1++ != *p2++) break; } } if (!*string++) break; } return NULL; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.