2 * Exercise 4-1. Write the function strrindex(s,t), which returns the position
3 * of the rightmost occurrence of t in s, or -1 if there is none.
8 #define MAXLINE 1000 /* maximum input line length */
10 int getlin(char line[], int max);
11 int strindex(char source[], char searchfor[]);
12 int strrindex(char s[], char t[]);
14 char pattern[] = "or"; /* pattern to search for */
16 /* find all lines matching pattern */
23 printf("pattern: %s\n", pattern);
24 while (getlin(line, MAXLINE) > 0)
25 if ((index = strrindex(line, pattern)) >= 0) {
26 printf("index: %d, %s", index, line);
32 /* getlin: get line into s, return length */
33 int getlin(char s[], int lim) {
36 while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
44 /* strindex: return index of t in s, -1 if none */
45 int strindex(char s[], char t[]) {
48 for (i = 0; s[i] != '\0'; i++) {
49 for (j=i, k=0; t[k] != '\0' && s[j] == t[k]; j++, k++)
51 if (k > 0 && t[k] == '\0')
57 /* strrindex: return index of rightmost t in s, -1 if none */
58 int strrindex(char s[], char t[]) {
60 for (i = strlen(s)-1; s[i] != '\0'; i--) {
61 for (j=i, k=0; t[k] != '\0' && s[j] == t[k]; j++, k++)
63 if (k > 0 && t[k] == '\0')