#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;
}
阅读(3215) | 评论(0) | 转发(0) |