实现字符串的查找和替换 分类: c/c++ 2014-10-09 22:33 469人阅读 评论(0) 收藏
2014-10-09 22:33
555 查看
在字符串中查找目标字符串并将其替换为指定字符串,返回替换的次数。接口为
int find_str_replace(char *&str,const char *find_str,const char *replace_str)
将str中所有find_str替换为replace_str。要求不利用STL,c实现代码如下:
int find_str_replace(char *&str,const char *find_str,const char *replace_str)
将str中所有find_str替换为replace_str。要求不利用STL,c实现代码如下:
#include<stdio.h> #include<string.h> #include<stdlib.h> //查找str从fromwhere开始第一次出现sub_str的位置 int find(const char *str,const char *sub_str,int fromwhere) { int len=strlen(str); int len_f=strlen(sub_str); for(int i=fromwhere;i<len&&len-i>=len_f;i++) { int k=i; int j=0; while(j<len_f) { if(str[k]==sub_str[j]) { k++; j++; } else break; } if(j==len_f) { return i; } } return -1; } //替换fromwhere处的find_str为replace_str void replace(char *&str,const char *find_str,const char *replace_str,int fromwhere) { int len=strlen(str); int len_f=strlen(find_str); int len_r=strlen(replace_str); char *p=(char*)malloc(len+len_r-len_f); memset(p,0,len+len_r-len_f); strncpy(p,str,fromwhere); strcat(p,replace_str); strcat(p,str+fromwhere+len_f); str=p; } //在str中将所有find_str替换为repalce_str; int find_str_replace(char *&str,const char *find_str,const char *replace_str) { int num=0; int len=strlen(str); int len_r=strlen(replace_str); int k=0; while(k<len) { int pos=find(str,find_str,k); if(pos==-1)break; else { replace(str,find_str,replace_str,pos); k=pos+len_r; num++; } } return num; } int main() { char *a="123456783450987634243453"; char *b="345"; char *c="ABCD"; int num=find_str_replace(a,b,c); printf("%s\n",a); printf("%d",num); return 0; }若使用c++并利用STL,代码极其简单:
#include<iostream> #include<string> using namespace std; int find_str_replace(string &str,string find_str,string replace_str) { int num=0; int len_f=find_str.length(); int len_r=replace_str.length(); int pos=str.find(find_str,0); while(pos!=string::npos) { str.replace(str.begin()+pos,str.begin()+pos+len_f,replace_str); pos=pos+len_r; pos=str.find(find_str,pos); num++; } return num; } int main() { string a="123456783450987634243453"; string b="345"; string c="ABCD"; int num=find_str_replace(a,b,c); printf("%s\n",a.c_str()); printf("%d",num); return 0; }
相关文章推荐
- C++实现快速排序(源代码) 标签: c++算法systemc 2011-11-09 22:01 75011人阅读 评论(11) 收藏 举报 分类: Algorithm(1) C/C++(13
- sed(查找替换) 与awk(提取字段) 分类: ubuntu 测试 2014-02-11 12:08 4074人阅读 评论(0) 收藏
- jQuery实现文本框回车键转tab键 分类: JavaScript 2012-12-06 16:19 1799人阅读 评论(0) 收藏
- 数字与字符串的比较 分类: python 2013-02-28 17:39 127人阅读 评论(0) 收藏
- C语言中的可变参数函数的浅析(以Arm 程序中的printf()函数实现为例) . 分类: HI3531 arm-linux-Ubuntu 2013-12-16 14:19 438人阅读 评论(0) 收藏
- ASP.NET中登录页验证码的实现 分类: ASP.NET 2012-03-08 22:44 4936人阅读 评论(5) 收藏
- 移除连续重复的字符串元素 分类: python 小练习 python基础学习 divide into python 2014-01-01 15:53 392人阅读 评论(0) 收藏
- jQuery实现文本框回车键转tab键 分类: JavaScript 2012-12-06 16:19 1800人阅读 评论(0) 收藏
- 字符串strip()方法注意事项 分类: python基础学习 2013-08-16 12:01 422人阅读 评论(0) 收藏
- 将aaaa替换成aaba 分类: python 小练习 2013-10-28 18:28 246人阅读 评论(0) 收藏
- #小练习 替换文件某行内容 分类: python 小练习 python Module 2013-09-26 11:10 269人阅读 评论(0) 收藏
- 用开源的 ffmpeg 实现屏幕录像机 分类: arm-linux-Ubuntu 2013-07-22 16:30 670人阅读 评论(0) 收藏
- 在文本框的特定位置用*号替换 分类: .NET 2012-04-28 11:13 493人阅读 评论(1) 收藏
- ucos在s3c2410上运行过程整体剖析---两种任务切换的实现方法 分类: μc /os ii 系统有关知识 2012-03-13 21:41 1102人阅读 评论(0) 收藏
- 基于DirectShow的MPEG-4视频传输系统的研究与实现 分类: VC++ DirectX 2013-11-09 09:40 630人阅读 评论(0) 收藏
- WebClient修改远程文件时:“远程服务器返回错误: (501) 未实现”的错误 分类: .NET 2012-11-17 16:01 3846人阅读 评论(0) 收藏
- 查找数组中最大的2个数 分类: B10_计算机基础 2013-02-11 19:52 1041人阅读 评论(1) 收藏
- Python修炼--常用字符串、数学函数 分类: python基础学习 2013-06-16 13:23 205人阅读 评论(0) 收藏
- str判断字符串方法 分类: python 2012-12-12 20:40 110人阅读 评论(0) 收藏
- NYOJ-469 擅长排列的小明 II AC 分类: NYOJ 2014-01-02 22:19 159人阅读 评论(0) 收藏