/* Exercise 4.1 from "The C programming language" book by K&R */
#include <stdio.h>
/* returns first right position of string t[] in string s[] or -1 if no entry */
int strrindex(char s[], char t[])
{
int len_s = 0, len_t = 0;
int p;
while (s[len_s++]); /*len_s = sizeof(s[])*/
while (t[len_t++]); /*len_t = sizeof(t[])*/
len_s -= 1;
len_t -= 1;
p = len_t; /*p = last symbol in t[]*/
while (len_s) {
len_t = pam;
while (s[--len_s] == t[--len_t]) {
if (!len_t)
return len_s;
if (!len_s)
return -1;
}
}
return -2;
}
int main(void)
{
char s[] = "abcdefabcdef";
char t[] = "cd";
printf("%d\n",strrindex(s,t));
return 0;
}
----------