当前位置:网站首页>[C language] realize string interception function
[C language] realize string interception function
2022-06-12 04:50:00 【ChenXiaoMing start】
1. Realization function : Enter a string , And input the left matching string and the right matching string , Intercept 2 Middle character function .
2. Code :
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(" The number of characters is - |%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<right_str_len;i++ ) {
aaa = (char*) strrchr(str, right_str[0]);
memset(str1,0,sizeof(str1) );
sprintf(str1,"%s",aaa);
ReplaceStr(str,str1,"");
}
//lr_error_message(" The string is - |%s|\n", aaa);
strcpy(ret,ret+left_str_len);
lr_error_message(" The string is - |%s|\n", str1);
ReplaceStr(ret,str1,"");
lr_error_message(" The string is - |%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. Output :
Starting action Action.
Action.c(20): Error: The string is - |.com|
Action.c(26): Error: The string is - |runoob.com|
Action.c(28): Error: The string is - ||
Action.c(32): Error: The string is - |runoob|
边栏推荐
- How to use union all in LINQ- How to use union all in LINQ?
- Let me tell you the benefits of code refactoring
- 22-2-28 there are many things to do at work today, ETH analysis
- Using datetime in MySQL
- LabVIEW关于TDMS和Binary存储速度
- Why is Julia so popular?
- 【cjson】根节点注意事项
- L1-067 Roche limit (10 points)
- kali_ Change_ Domestic source
- Bearpi IOT lighting LED
猜你喜欢

National land use data of 30m precision secondary classification

1009 word search

Why should a redis cluster use a reverse proxy? Just read this one

CCF access control system

How to make datasets, train them into models and deploy them based on yolov5
![[backtracking based on bit operation] queen n problem 2](/img/d3/25b2ba7c49ce0a9c1de26bc5c9497b.jpg)
[backtracking based on bit operation] queen n problem 2

Token based authentication
![[efficient] the most powerful development tool, ctool, is a compilation tool](/img/23/a5eb401affd64119590db273d60c23.png)
[efficient] the most powerful development tool, ctool, is a compilation tool

Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort

Jwt Learning and use
随机推荐
Ten trends of Internet Security in 2022 industry released
Difference between thread and task
Zabbix6.0 new feature GEOMAP map marker can you use it?
CCF access control system
2022 electrician (elementary) operation certificate examination question bank and online simulation examination
asp. Net core theme Middleware
[backtracking] backtracking to solve subset problems
Memory protection
Three. JS import model demo analysis (with notes)
C# TaskFactory. Startnew method
【C语言】实现字符串截取功能
Simulation of array implementation stack
Daily practice (28): balance binary tree
1007- stair climbing
Spatial distribution data of China's tertiary watershed / national new area distribution data /npp net primary productivity data / spatial distribution data of vegetation cover / land use data /ndvi d
Image processing 13- calculation of integral diagram
Raspberry pie 4B uses Intel movidius NCS 2 for inference acceleration
[wechat applet] the mobile terminal selects and publishes pictures
Install pycharm under Kali and create a shortcut access
【cjson】根节点注意事项