Question: In this program, a sample function strindex is given which has two parameters - char s[] and char t[]. It returns the left most position
In this program, a sample function strindex is given which has two parameters - char s[] and char t[]. It returns the left most position of t in s. You are going to complete the function strindexr which has two same parameters but returns the right most position of t in s. Return -1 if there is none. The main function is used to test your function. It will print out both the left and right most index of occurrence of pattern in line.
PROGRAM:
#include
int strindex (char source[], char searchfor[]); int strindexr (char source[], char searchfor[]);
char pattern[] = "ould"; /* pattern to search */
int main () { extern char pattern[]; char line[MAXLINE] = "I would would like to test this if i could";
printf ("strindex: %d\tstrindexr: %d ", strindex (line, pattern), strindexr (line, pattern));
return 0; }
int strindex (char s[], char t[]) { int i, j, k;
for (i = 0; s[i] != '\0'; i++) { for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++) ; if (k > 0 && t[k] == '\0') return i; } return -1; }
int strindexr (char source[], char searchfor[]) { // YOUR CODE HERE return -1; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
