Exercise 4-1. Write the function strindex(s,t) which returns the position of the rightmost occurrence of t in s, or -1 if there is none.
/* Exercise 4-1. Write the function strindex(s,t) which returns the position of the rightmost occurrence of t in s, or -1 if there is none. */#include<stdio.h>#include<string.h>#define MAXLINE 1000 /* maximum input line length */intgetsline(charline[],intmax);intstrindex(charsource[],charsearchfor[]);charpattern[]="bc";/* pattern to search for *//* find all lines matching pattern */intmain(){charline[MAXLINE];intfound=0;intpos=0;while(getsline(line,MAXLINE)>0){pos=strindex(line,pattern);if(pos>=0){printf("Rightmost occurene of %s in %s is = %d\n",pattern,line,pos);found++;}elseprintf("Match pattern not found !\n");}returnfound;}/* getsline: get line into s, return length */intgetsline(chars[],intlim){intc,i;i=0;while(--lim>0&&(c=getchar())!=EOF&&c!='\n')s[i++]=c;if(c=='\n')s[i++]=c;s[i]='\0';returni;}/* strindex: return position of the rightmost occurrence of t in s, -1 if none */intstrindex(chars[],chart[]){inti,j,k;for(i=strlen(s)-1;i>=0;i--){for(j=i,k=0;t[k]!='\0'&&s[j]==t[k];j++,k++);if(k>0&&t[k]=='\0')returni;}return-1;}