字符串匹配问题
2008-10-03 20:56
225 查看
问题:给定字符串“This is a string”,删除字符串中的“is”。
代码:
/*return the remainder string*/
char * matchstr(const char * src,const char *substr)
{
const char * p = substr;
int i = 0;
int src_len = strlen(src);
int sub_len = strlen(substr);
int buf_len = src_len;
char *tmp_buf = new char[src_len+1];
char * buf = NULL;
while(*src!='/0')
{
const char * ptmp = strstr(src,p);
if(ptmp!=NULL){
while(src<ptmp)//copy strings which is befor ptmp postion into tmp_buf.
{
tmp_buf[i]=*src;
++i;
++src;
}
src = src+sub_len;
}else{//if not match substr in src,then copy all string into tmp_buf
while(*src!='/0')
{
tmp_buf[i]=*src;
++i;
++src;
}
}
}
tmp_buf[i]='/0';
buf_len = strlen(tmp_buf);
buf = new char[buf_len+1];
strcpy(buf,tmp_buf);
delete[] tmp_buf;
return buf;
}
代码:
/*return the remainder string*/
char * matchstr(const char * src,const char *substr)
{
const char * p = substr;
int i = 0;
int src_len = strlen(src);
int sub_len = strlen(substr);
int buf_len = src_len;
char *tmp_buf = new char[src_len+1];
char * buf = NULL;
while(*src!='/0')
{
const char * ptmp = strstr(src,p);
if(ptmp!=NULL){
while(src<ptmp)//copy strings which is befor ptmp postion into tmp_buf.
{
tmp_buf[i]=*src;
++i;
++src;
}
src = src+sub_len;
}else{//if not match substr in src,then copy all string into tmp_buf
while(*src!='/0')
{
tmp_buf[i]=*src;
++i;
++src;
}
}
}
tmp_buf[i]='/0';
buf_len = strlen(tmp_buf);
buf = new char[buf_len+1];
strcpy(buf,tmp_buf);
delete[] tmp_buf;
return buf;
}
相关文章推荐
- 字符串转换成整数,字符串匹配问题
- 字符串匹配问题-KMP总结
- 用栈判断字符串匹配问题
- A - Translation字符串匹配问题
- 字符串匹配问题——求给定字符串的next数组以及KMP算法实现
- 字符串匹配问题
- poj-3461 kmp字符串匹配问题
- C++中用栈来判断括号字符串匹配问题
- 字符串转换成整数&带通配符的字符串匹配问题
- C语言 字符串匹配问题,KMP函数-失配函数
- 字符串匹配问题 kmp算法C语言实现
- 【庞果网英雄会】4月第4次面试集训#字符串#:字符串匹配问题
- Boyer-Moore(BM)算法,文本查找,字符串匹配问题
- HNU OJ题库1005F字符串匹配问题
- 字符串匹配问题:KMP算法
- 1005: 字符串匹配问题
- 字符串匹配问题
- 有限状态机求解字符串匹配问题
- 正则+split 解决国航项目获取字符串匹配问题
- 字符串匹配问题