
1.实现功能:输入一个字符串,并且输入左边匹配字符串和右边匹配字符串,截取2者中间字符功能。
2.代码:
Action()
{
char str[] = "http://www.runoob.com";
char left_str[] = "http://";
char right_str[] =".runoob.com";
str_save_param(str,left_str,right_str);
//lr_error_message("字符次数是 - |%d|\n", countchar(right_str,right_str[0]));
return 0;
}
void str_save_param(char * str,char left_str[],char right_str[]){
char *ret;
char *aaa;
char str1[] = "";
int left_str_len,right_str_len ;
int i;
left_str_len = strlen(left_str);
right_str_len = countchar(right_str,right_str[0]);
ret = (char*) strchr(str, left_str[0]);
for (i=0;i
aaa = (char*) strrchr(str, right_str[0]);
memset(str1,0,sizeof(str1) );
sprintf(str1,"%s",aaa);
ReplaceStr(str,str1,"");
}
//lr_error_message("字符串是 - |%s|\n", aaa);
strcpy(ret,ret+left_str_len);
lr_error_message("字符串是 - |%s|\n", str1);
ReplaceStr(ret,str1,"");
lr_error_message("字符串是 - |%s|\n", ret);
}
int ReplaceStr(char* sSrc, char* sMatchStr, char* sReplaceStr)
{
int StringLen;
char caNewString[1024];
char* findPos;
merc_timer_handle_t timer_ReplaceStr = lr_start_timer();
//lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function 'ReplaceStr' started.");
findPos =(char *)strstr(sSrc, sMatchStr);
if( (!findPos) || (!sMatchStr) ){
//lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function 'ReplaceStr' ended with error!");
return -1;
}
while( findPos)
{
memset(caNewString, 0, sizeof(caNewString));
StringLen = findPos - sSrc;
strncpy(caNewString, sSrc, StringLen);
strcat(caNewString, sReplaceStr);
strcat(caNewString, findPos + strlen(sMatchStr));
strcpy(sSrc, caNewString);
findPos =(char *)strstr(sSrc, sMatchStr);
}
//lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG,"Result:%s",sSrc);
free(findPos);
//lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function 'ReplaceStr' ended (Duration: %lf)",lr_end_timer(timer_ReplaceStr));
return 0;
}
int countchar(char *str,char a){
int n=0;
int i = 0;
while(*(str+i)!='\0'){
if(*(str+i) == a)
n++;
i++;
}
return n;
}
3.输出:
Starting action Action.
Action.c(20): Error: 字符串是 - |.com|
Action.c(26): Error: 字符串是 - |runoob.com|
Action.c(28): Error: 字符串是 - ||
Action.c(32): Error: 字符串是 - |runoob|
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)