c语言 把某一字符串中的其中一段字符串替换成另一串字符串。 c语言如何将一个字符串里某些字符代替另外一个字符串来

c\u8bed\u8a00\uff1a\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u7684\u5b57\u7b26\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u6307\u5b9a\u5b57\u7b26

/*\u529f\u80fd\uff1a\u5c06str\u5b57\u7b26\u4e32\u4e2d\u7684oldstr\u5b57\u7b26\u4e32\u66ff\u6362\u4e3anewstr\u5b57\u7b26\u4e32
*\u53c2\u6570\uff1astr\uff1a\u64cd\u4f5c\u76ee\u6807oldstr\uff1a\u88ab\u66ff\u6362\u8005newstr\uff1a\u66ff\u6362\u8005
*\u8fd4\u56de\u503c\uff1a\u8fd4\u56de\u66ff\u6362\u4e4b\u540e\u7684\u5b57\u7b26\u4e32
*\u7248\u672c\uff1aV0.2
*/
char*strrpc(char*str,char*oldstr,char*newstr){
char bstr[strlen(str)];//\u8f6c\u6362\u7f13\u51b2\u533a
memset(bstr,0,sizeof(bstr));
for(int i=0;i<strlen(str);i++){
if(!strncmp(str+i,oldstr,strlen(oldstr))){//\u67e5\u627e\u76ee\u6807\u5b57\u7b26\u4e32
strcat(bstr,newstr);
i+=strlen(oldstr)-1;
}else{
strncat(bstr,str+i,1);//\u4fdd\u5b58\u4e00\u5b57\u8282\u8fdb\u7f13\u51b2\u533a
}
}
strcpy(str,bstr);
return str;
}

\u6269\u5c55\u8d44\u6599\uff1ac_str\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662fconstchar*\u7684\uff0c\u4e0d\u80fd\u76f4\u63a5\u8d4b\u503c\u7ed9char*\uff0c\u6240\u4ee5\u5c31\u9700\u8981\u6211\u4eec\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u8f6c\u5316\uff0c\u4e0b\u9762\u5c31\u662f\u8fd9\u4e00\u8f6c\u5316\u8fc7\u7a0b\u3002
c++\u8bed\u8a00\u63d0\u4f9b\u4e86\u4e24\u79cd\u5b57\u7b26\u4e32\u5b9e\u73b0\uff0c\u5176\u4e2d\u8f83\u539f\u59cb\u7684\u4e00\u79cd\u53ea\u662f\u5b57\u7b26\u4e32\u7684c\u8bed\u8a00\u5b9e\u73b0\u3002\u4e0eC\u8bed\u8a00\u7684\u5176\u4ed6\u90e8\u5206\u4e00\u6837\uff0c\u5b83\u5728c++\u7684\u6240\u6709\u5b9e\u73b0\u4e2d\u53ef\u7528\uff0c\u6211\u4eec\u5c06\u8fd9\u79cd\u5b9e\u73b0\u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u5f52\u4e3ac-\u4e32\uff0c\u6bcf\u4e2ac-\u4e32char*\u7c7b\u578b\u7684\u3002
\u6807\u51c6\u5934\u6587\u4ef6<cstring>\u5305\u542b\u64cd\u4f5cc-\u4e32\u7684\u51fd\u6570\u5e93\u3002\u8fd9\u4e9b\u5e93\u51fd\u6570\u8868\u8fbe\u4e86\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u7684\u51e0\u4e4e\u6bcf\u79cd\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002
\u5f53\u8c03\u7528\u5e93\u51fd\u6570\uff0c\u5ba2\u6237\u7a0b\u5e8f\u63d0\u4f9b\u7684\u662fstring\u7c7b\u578b\u53c2\u6570\uff0c\u800c\u5e93\u51fd\u6570\u5185\u90e8\u5b9e\u73b0\u7528\u7684\u662fc-\u4e32\uff0c\u56e0\u6b64\u9700\u8981\u5c06string\u5bf9\u8c61\uff0c\u8f6c\u5316\u4e3achar*\u5bf9\u8c61\uff0c\u800cc_str()\u63d0\u4f9b\u4e86\u8fd9\u6837\u4e00\u79cd\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56deconst char*\u7c7b\u578b(\u53ef\u8bfb\u4e0d\u53ef\u6539)\u7684\u6307\u5411\u5b57\u7b26\u6570\u7ec4\u7684\u6307\u9488\u3002\u4f8b\uff1a
#include<iostream>
//#include<cstring>
#include<string>//\u4f7f\u7528\u5934\u6587\u4ef6string,\u5426\u5219\u6700\u540ecout<<\u65e0\u6cd5\u8f93\u51faadd_to
using namespace std;
int main()
{
string add_to="hello!";
const string add_on="baby";
const char*cfirst=add_to.c_str();
const char*csecond=add_on.c_str();
char*copy=new char[strlen(cfirst)+strlen(csecond)+1];
strcpy(copy,cfirst);
strcat(copy,csecond);
add_to=copy;
cout<<"copy:"<<copy<<endl;
delete[]copy;
cout<<"add_to:"<<add_to<<endl;
return 0;
}
\u4f8b(1)
\u51fd\u6570\u58f0\u660e\uff1aconst char*c_str();
c_str()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u6b63\u89c4C\u5b57\u7b26\u4e32\u7684\u6307\u9488,\u5185\u5bb9\u4e0e\u672cstring\u4e32\u76f8\u540c.
\u8fd9\u662f\u4e3a\u4e86\u4e0ec\u8bed\u8a00\u517c\u5bb9\uff0c\u5728c\u8bed\u8a00\u4e2d\u6ca1\u6709string\u7c7b\u578b\uff0c\u6545\u5fc5\u987b\u901a\u8fc7string\u7c7b\u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570c_str()\u628astring\u5bf9\u8c61\u8f6c\u6362\u6210c\u4e2d\u7684\u5b57\u7b26\u4e32\u6837\u5f0f\u3002
\u6ce8\u610f\uff1a\u4e00\u5b9a\u8981\u4f7f\u7528strcpy()\u51fd\u6570\u7b49\u6765\u64cd\u4f5c\u65b9\u6cd5c_str()\u8fd4\u56de\u7684\u6307\u9488
\u6bd4\u5982\uff1a\u6700\u597d\u4e0d\u8981\u8fd9\u6837:
char*c;
string s="1234";
c=s.c_str();
//c\u6700\u540e\u6307\u5411\u7684\u5185\u5bb9\u662f\u5783\u573e\uff0c\u56e0\u4e3as\u5bf9\u8c61\u88ab\u6790\u6784\uff0c\u5176\u5185\u5bb9\u88ab\u5904\u7406\uff08\u7ea0\u6b63\uff1as\u5bf9\u8c61\u7684\u6790\u6784\u662f\u5728s\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\uff0c\u4f8b\u5982\u51fd\u6570\u7684\u8fd4\u56de\uff09
\u53c2\u8003\u8d44\u6599\uff1a
\u767e\u5ea6\u767e\u79d1\u2014\u2014c_str

\u51e0\u5929\u524d\u56de\u7b54\u8fc7\u4e00\u6837\u7684\u95ee\u9898\uff0c\u6570\u5b57\u4e5f\u53ef\u4ee5\u7684\uff0c\u6211\u6d4b\u8bd5\u8fc7\u4e86\u3002\u957f\u5ea6\u4e0d\u540c\u4e5f\u53ef\u4ee5\u3002
#include
#include
main()
{ char string[300],change[50],replace[50];
char s[300];
char *p,*q;
int i,lengthch,lengthre,flag,j=0;
printf("Input a string:");
gets(string);
printf("Input the string you want to be changed:");
scanf("%s",change);
printf("Input the string you want to replace:");
scanf("%s",replace);
lengthch=strlen(change);
lengthre=strlen(replace);
puts(string);
p=q=string;
for(;*p;p++)
{if(*p==change[0])
{
flag=1;
q=p;
for(i=0;i<lengthch;i++)
{ if(*q!=change[i])
{ flag=0;
break;
}
else q++;
}
if(flag==1)
{ for(i=0;i<lengthre;i++)
s[j++]=replace[i];
p=q-1;
}
}
else s[j++]=*p;
}
s[j]='\0';
puts(s);
}

这个写一个算法吧,搜索字符串,寻找"b",找到后判断b的后一个字符是否为"c",如果不是c,则继续搜索直到字符串结束,如果为c,那么将bc替换为xy。

#include
<stdio.h>
#include
<string.h>
void
main(void)
{
char
getstr[100];
char
sendstr[100];
char
a_b[100];
char
i;
printf("please
input
the
firs
str!\n");
gets(getstr);
printf("please
input
the
second
str
!\n");
gets(sendstr);
strcpy(a_b,getstr);
//交换两个字符串的数据
strcpy(getstr,sendstr);
strcpy(sendstr,a_b);
for(i
=
0;
getstr
!=
'\0';
i++)
{
printf("str1:%c",getstr[i]);
}
for(i
=
0;
getstr
!=
'\0';
i++)
{
printf("str2:%c",sendstr[i]);
}
}
这是将两个字符串进行交换后输出。
第二个问题则用到了数据结构了,使用一个结构体链表,使用strcmp()或strcmpi()比对你输入的字符串,找到相同的输出对应的中文字符串就好了。
其中:
strcmp()
对两个字符串进行大小写敏感的比较;strcmpi()
对两个字符串进行大小写不敏感的比较;

char a[]="abcdef";
char *q=strstr(a,"bc");
strncpy(q,"xy",2);
puts(a);

char str[]="abcdef";
str[1]='x';
str[2]='y';

我不会

扩展阅读:c++中<< ... cpp string截取一部分 ... c截取字符串中特定部分 ... c语言 字符串怎么声明 ... c语言字符长度怎么看 ... sql取字符串的某一部分 ... c语言字符拼接在一起 ... vb中截取特定的字符串 ... c语言截取字符串的某一段 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网