当前位置:网站首页>【C语言】实现字符串截取功能
【C语言】实现字符串截取功能
2022-06-12 04:48:00 【陈晓明start】
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<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("字符串是 - |%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|
边栏推荐
- A complete set of installation procedures (for learning and communication only)
- C# TaskFactory. Startnew method
- [backtracking method] queen n problem
- Epidemic data analysis platform work report [2] interface API
- Day18 creation and restoration of sparse array
- EnterpriseTECH STAR Question
- How to deploy PostgreSQL as a docker container
- Install/Remove of the Service Denied!
- LabVIEW about TDMS and Binary Storage Speed
- How to construct a search string?
猜你喜欢

Zabbix6.0 new feature GEOMAP map marker can you use it?

Install pycharm under Kali and create a shortcut access

QT compiling security video monitoring system 43- picture playback

存储器的保护

22-2-28 there are many things to do at work today, ETH analysis

QT compile 45 graphic report of security video monitoring system

Ten trends of Internet Security in 2022 industry released

疫情数据分析平台工作报告【3】网站部署

New year news of osdu open underground data space Forum

Simple Tetris
随机推荐
According to aiqicha, keep went public in Hong Kong and hit the "first share of online fitness"
MySQL master-slave construction and Django implementation of read-write separation
Based on Visual Studio code Net Maui cross platform mobile application development
Some problems of silly girl solved
Unable to resolve dependency tree
Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort
22-2-28 there are many things to do at work today, ETH analysis
加速訓練之並行化 tf.data.Dataset 生成器
Gavin teacher's perception of transformer live class - rasa dialogue robot project practice in the field of education agency mode and core component source code analysis under the microservice of educ
EnterpriseTECH STAR Question
疫情数据分析平台工作报告【4】跨域相关
Work report of epidemic data analysis platform [42] codenet
2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units
L1-064 AI core code valued at 100 million (20 points)
2022 self study materials for Zhejiang computer level III network and security technology examination (1) (updated on 2.28)
Kwai opens a milestone activity for fans to record every achievement moment for creators
[wechat applet] the mobile terminal selects and publishes pictures
Work report of epidemic data analysis platform [4] cross domain correlation
JWT learning and use
Operation of simulated examination platform for theoretical question bank of G2 utility boiler stoker in 2022