Encoders
Hub for Computer Whizzes Register to join us

Encoders > More... > C > Program

/* Function strindex(s,t) which returns the position of the rightmost
   occurence of t in s, or -1 if there is none. */

#include <stdio.h>

int strindex(char s[], char t[]);

main()
{
        char a[]="hello world", b[]="orl";
        printf("%d\n",strindex(a,b));
}


int strindex(char s[], char t[])
{
	int i, j;
	for(i=0;s[i]!='\0';i++)
		;
        for(; i>=0 ; i= i-j-1)
	{
                for(j=0 ; s[i]!='\0' && t[j]!='\0' && s[i]==t[j]; ++i, ++j)
			;
                if(j>0 && t[j]=='\0')
			return i-j;
	}
	return -1;
}