一道面试题,字符串的查找与替换?

whizstorm 2004-11-20 10:09:55
比如,将字符串strSrc中的含"abc"的部分全部换成"XXXX" 等
char *strSrc;
const char *p="abc";
const char *q="XXXX";
...全文
848 29 打赏 收藏 转发到动态 举报
写回复
用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)

64,266

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧