社区
C++ 语言
帖子详情
一道面试题,字符串的查找与替换?
whizstorm
2004-11-20 10:09:55
比如,将字符串strSrc中的含"abc"的部分全部换成"XXXX" 等
char *strSrc;
const char *p="abc";
const char *q="XXXX";
...全文
946
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)
剑指offer-复习c++
面试题
5
替换
空格。
面试题
5
替换
空格。
字符串
复习 c语言
字符串
就是比字符数组多一个字节,结尾为’\0’ c
字符串
" 1.长度 strlen(char* s) 2.比较 strcmp(char*s1, char*s2) //返回int strncmp(char*s1, char*s2, int n) //比较前n个字符 3.附加 strncat(char* dest, char* src, int n)/...
【C++深度剖析学习总结】 26 C++ 中的
字符串
类
字符串
流类(sstream)用于string的转换—支持
字符串
到数字的转换。问题:C++中的原生类型系统是否包含
字符串
类型?C语言不支持自定义类型,因此无法获得
字符串
类型。标准库中提供了相关的类对
字符串
和数字进行转换。
字符串
类的使用—目的:知道
字符串
的基本操作。标准库中通过string类支持
字符串
的概念。在C++中可以通过类完成
字符串
类型的定义。string类支持
字符串
和数字的相互转换。string直接支持
字符串
的插入和
替换
。string直接支持
字符串
的大小比较。C++中没有直接支持原生的
字符串
类型。
C语言之
字符串
搜索以及
替换
字符串
搜索以及
替换
刚开始摸爬打滚的登上了求职的路上,经历了无数的面试以及笔试。至今还没有着落。经验已经积累了一些,今天在这里写详细分析一下我在笔试当中遇到的一个高频考题。没错,就是本章的题目,
字符串
的搜索以及
替换
。 题目介绍 目前参过4个不同公司企业的笔试了。其中这一题居然连续出现了两次。这一题比较考基础以及程序员的思维。看似简单但暗藏的玄机,如果不在考试之前做好充分的准备,那么在半个小时或一个...
[LeetCode] Find And Replace in String 在
字符串
中
查找
和
替换
To some stringS, we will perform somereplacementoperations that replace groups of letters with new ones (not necessarily the same size). Each replacement operation has3parameters: a starting ...
秋招题目:
字符串
替换
函数-----C语言实现
输入一个以回车结束的
字符串
(少于80个字符),将其中大写字符用下面列出的大写字母
替换
,其余字符不变。输出
替换
后的
字符串
。试编写相应的程序。(提示:每对相互转换的字符和相等) A------->Z B------->Y C------->X … X------->C Y------->B Z------->A 输入格式: 输入一行字符以回车结束 输出格式: 把对应的大写中字符转换,其余字符不变 输入样例: 123ABXyu 输出样例: 123ZYCyu 个人理解:首先定义三
C++ 语言
65,211
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章