练习4-1

来源:互联网 发布:dnf优化2017 编辑:程序博客网 时间:2024/06/02 22:49

#include <stdio.h>
/*
strrindex这个函数返回字符串t在s中最右边出现的位置.如果s中不包含t,则返回-1.
*/
int comp_s(const char *s,const char *t)
{ /*如果s包含t则返回1,否则返回0 */
 for(; *t !='/0' ;t++){
  if(*t !=*(s++))
   return 0;
 }
 return 1;
}
  
int strrindex(const char *s,const char *t)
{
 const char *s_iter=s, *t_iter=t;
 int   index =-1;
 while(*s_iter !='/0')
  s_iter++,index++;
 s_iter--;

 while( s_iter-- != s){
  index--;
  if( *s_iter ==*t)
   if( comp_s( s_iter,t) )return  index;
 }
 return -1;/* s中不包含t */

}
int main()
{
 char *a="asdkfjwieofjwieojfwjfe";
 char *b="i";
 printf("%d/n",strrindex(a,b));
 return 0;
}

原创粉丝点击