Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3349466
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: C/C++

2006-04-06 15:58:36

#include
#include
/*
 function : get the position of keyword in a given string
 input : the original string , keyword of searching
 output : the position of keyword in given string
*/
int poslastkey(const char *str, const char *strkey)
{
 int iLen, ikeyLen;
 int iPos;
 
 if(str == NULL || strkey == NULL)
  return -1;//输入参数有误
 iLen = strlen(str);
 ikeyLen = strlen(strkey);
 iPos = (unsigned int)str + iLen - ikeyLen;
 
 //对字符串进行比较
 while(strncmp((char *)iPos, strkey, ikeyLen)!=0 && iPos >= (unsigned int)str)
   --iPos;
 
 if(iPos < (unsigned int)str)
  return -2; //在串中没有找到输入的关键字
 return (iPos - (unsigned int)str);
}
/*
 function : get the last string according the key word
 input : the original string , keyword of searching
 output : the word from the keyword to the end of string
*/
char* laststr(const char *str, const char *strkey)
{
 int ikeyLen;
 int iPos;
   
 if(str == NULL || strkey == NULL)
  return NULL;
 
 iPos = poslastkey(str, strkey);
 if(iPos < 0)
 {
  return NULL;
 }
 else
 {
  ikeyLen = strlen(strkey);
  iPos = (unsigned int)str + iPos + ikeyLen;
  
  return (char *)iPos;
 }
}

int main(int argc, char *argv[])
{
 printf("%s", laststr("12345", "5"));
 return 0;
}
阅读(3066) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~