题目描述:字符串查找

要求: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\n",strindex(str1,str2));
}

 


批注:空循环用的好啊。

posted @ 2013-07-03 21:28  爱生活,爱编程  阅读(206)  评论(0编辑  收藏  举报