社区
C++ 语言
帖子详情
一道面试题,字符串的查找与替换?
whizstorm
2004-11-20 10:09:55
比如,将字符串strSrc中的含"abc"的部分全部换成"XXXX" 等
char *strSrc;
const char *p="abc";
const char *q="XXXX";
...全文
849
29
打赏
收藏
一道面试题,字符串的查找与替换?
比如,将字符串strSrc中的含"abc"的部分全部换成"XXXX" 等 char *strSrc; const char *p="abc"; const char *q="XXXX";
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
29 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
masse
2004-12-01
打赏
举报
回复
我写了一个,比较郁闷的,
只用了strlen,没有用其他任何库函数,
能够运行
VC6.0下通过
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* Str(char *strSrc,const char *p,const char *q)
{
int lenSrc = strlen(strSrc);
int lenp = strlen(p);
int lenq = strlen(q);
int len = lenSrc*lenq/lenp;
int num = 0;
char* result = new char[len];
for(int i = 0 ;i <= lenSrc-lenp+1; i++)
{
int flag = 1;
{
for(int j = 0;j<lenp ; j++)
if(strSrc[i+j]!=p[j])
{
flag = 0;
break;
}
}
if(flag==1)
{
for(int j = 0;j<lenq ; j++)
{
result[num] = q[j];
num++;
}
i = i + lenp -1;
}
else
{
result[num] = strSrc[i];
num++;
}
}
result[num] = '\0';
return result;
}
void main()
{
char* a={"ft"};
printf("%s\n",Str(a,"ft","kao"));
}
pcboyxhy
2004-11-30
打赏
举报
回复
KMP查找匹配,
然后替换
AIGPTchina
2004-11-28
打赏
举报
回复
怎样把一个指向字符的 指针数组赋给字符数组???
怎样把 一个指向字符的 指针数组赋给 结构体?????
那位可以告诉我!在下谢谢!
char *pip[]={"oeioie","ifefd"};
char *ii[]={"fhjj","dkkj"};
struct pop{
char *oo;//如果写成这样有不行 char *oo[];,
char *yy;
}ww[52]={{pip[0],ii[0]},
…………………
…………………
…………………
};
但是不行 ,只能赋一个?
在下谢谢拉!!发我的e_mail:wcg_jishuo@163.com QQ:278359100
timfun
2004-11-25
打赏
举报
回复
出题者的意思应该是要不用库。。
dot99
2004-11-25
打赏
举报
回复
std::find_if
weqi
2004-11-22
打赏
举报
回复
Mark
realee
2004-11-22
打赏
举报
回复
:)
whizstorm
2004-11-22
打赏
举报
回复
命题者的意思是 写一个通用的字符串的查找与替换函数,不要用库函数。
superwyf
2004-11-22
打赏
举报
回复
上面代码自己写string的时候写的,也不知道真正的应该怎么写!
不过经理说这东西不合平常人的习惯!
上面代码我运行过,不过不知道还有什么“八哥”没有?
请高手指正!
superwyf
2004-11-22
打赏
举报
回复
////////////////////////////////////
// 功能函数 //
////////////////////////////////////
//此处的oc为原字符串中的将被替换的一段字符串的内容,nc为新的替代字符串,
//此处的s为在字符串中检索的起点索引,e为在字符串中检索的终点索引。
str& str::Replace( const char* oc, const char* nc, int s, int e)
{
assert( strlen(oc) == strlen(nc));
if(e==0)
{
e=len-1; //e等于最后一个元素的索引
}
assert( s>=0 && e<len && s<=e);
int count=0;
for(int i=s; i<(e+1-strlen(oc)+1); i++)
{
count=0;
for(int j=i; j<i+strlen(oc); j++)
{
if(m_date[j] != oc[j-i])
{
break;
}
count+=1;
}
if(count == strlen(oc))
{
for(int k=i; k<i+strlen(oc); k++)
{
m_date[k]=nc[k-i];
}
i+=strlen(oc)-1;
}
}
return *this;
}
//此处的oc为原字符串中的将被替换的一段字符串的内容,nc为新的替代字符串,
//此处的s为在字符串中检索的起点索引,e为在字符串中检索的终点索引。
str& str::Replace( const char* oc, const str & nc, int s, int e)
{
assert(strlen(oc)==nc.getlen());
if(e == 0)
{
e=len-1; //e等于最后一个元素的索引
}
assert( s>=0 && e<len && s<=e);
int count=0;
for(int i=s; i<(e+1-strlen(oc)+1); i++)
{
count=0;
for(int j=i; j<i+strlen(oc); j++)
{
if(m_date[j] != oc[j-i])
{
break;
}
count+=1;
}
if(count == strlen(oc))
{
for(int k=i; k<i+strlen(oc); k++)
{
m_date[k]=nc[k-i];
}
i+=strlen(oc)-1;
}
}
return *this;
}
//此处的oc为原字符串中的将被替换的一段字符串的内容,nc为新的替代字符串,
//此处的s为在字符串中检索的起点索引,e为在字符串中检索的终点索引。
str& str::Replace( const str & oc, const char* nc, int s, int e)
{
assert(oc.getlen() == strlen(nc));
if(e==0)
{
e=len-1; //e等于最后一个元素的索引
}
assert( s>=0 && e<len && s<=e);
int count=0;
for(int i=s; i<(e+1-oc.getlen()+1); i++)
{
count=0;
for(int j=i; j<i+oc.getlen(); j++)
{
if(m_date[j] != oc[j-i])
{
break;
}
count+=1;
}
if(count==oc.getlen())
{
for(int k=i; k<i+oc.getlen(); k++)
{
m_date[k]=nc[k-i];
}
i+=oc.getlen()-1;
}
}
return *this;
}
//此处的oc为原字符串中的将被替换的一段字符串的内容,nc为新的替代字符串,
//此处的s为在字符串中检索的起点索引,e为在字符串中检索的终点索引。
str& str::Replace( const str & oc, const str & nc, int s, int e)
{
return Replace(oc.getstr(), nc.getstr(), s, e);
}
hxy2003
2004-11-22
打赏
举报
回复
UP
BinaryWorld
2004-11-22
打赏
举报
回复
模式匹配!
wangchongran
2004-11-22
打赏
举报
回复
我是学计算机专业的,面试的题这么难呀!!以后我可怎么找工作啊!!!
zblaoshu1979
2004-11-22
打赏
举报
回复
mark
pt2000good
2004-11-22
打赏
举报
回复
#include <iostream>
using namespace std;
#include <cstddef>
#include <cstring>
char *copy(char *,const char*,const char*,unsigned int);
const char *p ="abc";
const char *q="xxxx";
int main(void)
{
unsigned int n;
char sentence[20];
cout<<"enter sentence:"<<endl;
cin.getline(sentence,20);
n=strlen(sentence);
char*result;
result=copy(sentence,p,q,n);
cout<<"the new result:"<<result<<endl;
return 0;
}
char *copy(char *a,const char *b,const char *c,unsigned int lenth)
{
for(unsigned int i=0;i<lenth-2;i++)
{
if(a[i]==b[0])
{
if(a[i+1]==b[1]&&a[i+2]==b[2])
{
lenth++;
a[i]=c[0];
a[i+1]=c[1];
a[i+2]=c[2];
for(unsigned int j=lenth-1;j>i+2;j--)
{
a[j+1]=a[j];
}
a[i+3]=c[3];
}
}
continue;
}
return a;
}
Andy84920
2004-11-21
打赏
举报
回复
/*
比如,将字符串strSrc中的含"abc"的部分全部换成"XXXX" 等
char *strSrc;
const char *p="abc";
const char *q="XXXX";
*/
#include <iostream>
using namespace std;
#include <string>
#include <cstring>
#include <algorithm>
//此函数将source串中dest串全部替换成newstr串.
void string_replace(char *source, const char *dest, const char *newstr)
{
string temp(source);
// use string::size_type
string::size_type pos=0;
while( (pos = temp.find(dest,pos)),(pos != string::npos) ) //note: , expression
{
temp.replace(pos,strlen(dest),newstr);
pos += strlen(newstr); //note: not pos+=strlen(dest);
}
strcpy(source,temp.c_str());
}
int main()
{
char a[80]="abcdabcddabc";
const char *dest="abc";
const char *newstr="xxxx";
cout << a << endl;
string_replace(a,dest,newstr); //注意:最终替换后的串一定要能全部放入a中.
cout << a << endl;
cin.get();
}
Squall1009
2004-11-21
打赏
举报
回复
用内存比较和内存拷贝吧^^
tiefan
2004-11-21
打赏
举报
回复
要求高手把代码写出来
fcf128
2004-11-21
打赏
举报
回复
STL: find()算法就可以实现
whizstorm
2004-11-21
打赏
举报
回复
要求高手把代码写出来
加载更多回复(9)
PHP7
字符串
模块
King老师精心打造的PHP7
字符串
实战课程,由浅入深的讲解了PHP中
字符串
模块,并介绍了一些常用的
字符串
相关操作,让我们一起开启
字符串
之旅吧~ 山哥出品,必属精品!
C语言之
字符串
搜索以及
替换
字符串
搜索以及
替换
刚开始摸爬打滚的登上了求职的路上,经历了无数的面试以及笔试。至今还没有着落。经验已经积累了一些,今天在这里写详细分析一下我在笔试当中遇到的一个高频考题。没错,就是本章的题目,
字符串
...
字符串
替换
:用参数字符数组成员
替换
字符串
中的占位符(
面试题
)
1、问题描述: ...给定一个
字符串
A,同时给定它的长度n及参数字符数组arg和它的大小m,请返回
替换
后的
字符串
。保证参 数个数大于等于占位符个数。保证原串由大小写英文字母组成,同时长度小于等于500。
【c++】
字符串
替换
:——
面试题
请你实现一个简单的
字符串
替换
函数。原串中需要
替换
的占位符为”%s”,请按照参数列表的顺序一一
替换
占位符。若参数列表的字符数大于占位符个数。则将剩下的参数字符添加到
字符串
的结尾。给定一个
字符串
A,同时给定它...
字符串
常见
面试题
很多其他类型的
面试题
可看作是
字符串
类型的
面试题
需要掌握的概念 回文 子串(连续) 子序列(不连续) 前缀树(Trie 树) 后缀树和后缀数组 匹配 字典序 需要掌握的操作 与数组有关的操作:增删改查 字符
替换
...
C++ 语言
64,635
社区成员
250,963
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章