要求:Write the function strindex(s,t) which returns the position of the rightmost occurrence of
t in s, or -1 if there is none.
the c programming language second edittion
#include <stdlib.h> int strindex(char *src_str,char *dest_str) { int i,j,k; int src_length=strlen(src_str)-1; int dest_length=strlen(dest_str)-1; for(i=src_length;i>=0;--i) { for(j=i,k=dest_length;k>=0&&dest_str[k]==src_str[j];--j,--k) ; if(k < 0) return i-dest_length; } return -1; } main() { char str1[20]="ABCDABSCD"; char str2[20]="CD"; printf("%d ",strindex(str1,str2)); }
批注:空循环用的好啊。