输入3个字符串,在第一个字符串中查找第二个字符串,并用第三个字符串替换 用C语言编写程序,从键盘输入两个字符串,输出第一个字符串在第...

作者&投稿:包翁 (若有异议请与网页底部的电邮联系)
语言:C++

a、利用临时变量存储结果
#include <iostream>
#include <string>
using namespace std;
string replaceStr(string a, string b, string c)
{
string result="";
unsigned int alength=a.length();
unsigned int blength=b.length();

int tempi=0;
for(int i=0;i<alength-blength+1;i++)
{

if(a[i]==b[tempi])
{
tempi++;

if(tempi==blength)
{
result.append(c);
tempi=0;
}
}
else
{
if(tempi!=0)
{
i=i-tempi;
tempi=0;
}
result.append(1,a[i]);

}

}
result.append(a.substr(i,alength-i));
return result;
}

void main()
{
string a,b,c;
cout<<"please input the source string: >";
cin>>a;
cout<<"please input the string to be replaced: >";
cin>>b;
cout<<"please input the string to be replaced with: >";
cin>>c;

cout<<endl<<"the source string is: "<<a<<endl;
cout<<"the result string is: "<<replaceStr(a,b,c)<<endl;
}

b、直接修改原字符串

#include <iostream>
#include <string>
using namespace std;
void replaceStr(string& a, string b, string c)
{
unsigned int alength=a.length();
unsigned int blength=b.length();
unsigned int clength=c.length();

int tempi=0;
for(int i=0;i<alength-blength+1;i++)
{

if(a[i]==b[tempi])
{
tempi++;

if(tempi==blength)
{
a=a.substr(0,i-tempi+1)+c+a.substr(i+1,alength-i-1);
alength=a.length();
i=i+clength-blength;
tempi=0;
}
}
else
{
if(tempi!=0)
{
i=i-tempi;
tempi=0;
}
}

}
}

void main()
{
string a,b,c;
cout<<"please input the source string: >";
cin>>a;
cout<<"please input the string to be replaced: >";
cin>>b;
cout<<"please input the string to be replaced with: >";
cin>>c;

replaceStr(a,b,c);
cout<<"the result string is: "<<a<<endl;
}

我有一个用C#写的
public static void Main(string[] args)
{
Console.WriteLine("Input first string");
string first = Console.ReadLine();
Console.WriteLine("Input second string");
string second = Console.ReadLine();
Console.WriteLine("Input third string");
string third = Console.ReadLine();

first = first.Replace(second, third);

Console.WriteLine("The result is :" + first);
}

=REPLACE(A1,2,1,RIGHT(A1))
这样?

从键盘输入3个字符串,把第一个字符串中所有出现的第二个字符串全部替换为第三个字符串.~

#include#includeint main(void){char a[15], b[15], c[15];int i, j, k;scanf("%s", a);scanf("%s", b);scanf("%s", c);for(i = 0; i < strlen(a); i++){for(j = 0; j < strlen(b); j++){if(a[i] != b[j])break;}for(j = 0; j < strlen(b); j++){a[i] = c[j];}i += strlen(b);}printf("
%s", a);return 0;}

#include int main(){ char str1[100],str2[100]; char *p,*p1,*p2; gets(str1); gets(str2); for(p=str1;*p!='\0';p++) { for(p1=p;p2=str2;*p1==*p2;p1++,p2++); if(*p2=='\0') { printf("%d",p-str1); return 0; } } return 0;}

输入三个字符串,按从小到大的顺序输出。
答:if(strcmp(str1,str3)>0)swap(str1,str3);if(strcmp(str2,str3)>0)swap(str2,str3);printf("After exchange:");printf("%s\n%s\n%s\n",str1,str2,str3);} void swap(char *s1,char *s2){ char p[20];strcpy(p,s1);//strcpy拷贝字符串的函数 strcpy(s1,s2);strcpy(s2,p)...

(c语言)输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符...
答:#include"stdio.h"charmax(chara,charb){ if(a>b){ returna;} else { returnb;} } charmin(chara,charb){ if(a<b){ returna;} else { returnb;} } intmain(intargc,charconst*argv[]){ charc0,c1,c2,c3,c4,c5,c6,c7;printf("Pleaseinput...

使用命令行参数读入三个字符串,第三个字符串为一个带目录的文件名,如...
答:这是代码:public class StringDemo2 {public static void main(String[] args) throws Exception {if (args.length < 3) {System.out.println("参数个数不够!");return;}if (args[0].contains(args[1])) {System.out.println("true");} else {System.out.println("false");}String file...

用C语言输入3个字符串,并找出其中的最大值?
答:返回最大的那个数组下标int main(){ char str[3][10]; int indext; int len[3];//3个字符串长度 int sum[3];//3个字符串的ASC码总和 printf("测试:输入3个字符串(最多9个字符):\n"); scanf("%s",&str[0][0]); scanf("%s",&str[1][...

在C++环境下如何输入3个字符串,按由大到小的顺序输出
答:include<string.h> include<iostream.h> void main(){ int i,j;string a[3],temp;for(i=0;i<3;i++)cin>>a[i];for(i=0;i<2;i++)for(j=i;j<2;j++)if(a[j].length<a[j+1].length){ temp=a[j];a[j]=a[j+1];a[j+1]=temp;} for(i=0;i<3;i++)cout<<a...

C语言,在C++环境下运行“输入3个字符串,按由小到大的顺序输出”用指针的...
答:return -1;} return 1;} void strcpy(char *s1, char *s2){ int i=0;while(*(s2+i)!='\0'){ (s1+i)=*(s2+i);i++;} (s1+i)='\0';} void main(){ char s1[50],s2[50],s3[50];char s[50];printf("输入三个字符串:\n");gets(s1);gets(s2);gets(s3);if(...

...可以显示出第二个字符串在第一个字符串中出现的位置和次数。_百度...
答:p=str1;q=str2;do { if(*p!=*q){ p++;lct++;} else { while((*q!='\0')&&(*q==*p)){ q++;p++;lct++;} if(*q=='\0'){ locat[i]=lct-len;i++;} } q=str2;} while(*p!='\0');if(i==0){printf("no");} else {printf("字符串2在字符串1中出现%d次,...

python用户输入一个长度为3的字符串,求该字符串中各字符ascii码之和...
答:print sum(map(ord,list(str1)))计算各位数字之和:print sum(map(ord,list(str1)))-96*len(str1)其中str1是输入字符串 以123为例 list(str1)将字符串变成 ['1','2','3']ord 将字母变成ASCII 数字 map(ord,list(str1))将列表中所有字母变成数字[49,50,51]sum 求和 这是第一小问...

从键盘输入两个字符串,输出第一个字符串在第二个字符串中的位置_百度知...
答:int nStringBLength = strlen( pszStringB ); // 求得字符串 B 的长度 int i = 0;// 遍历字符串 B 里面的每一个字符 for( i = 0; i < nStringBLength; i++ ){ // 这里不用解释了吧 // 就是用字符串 A 里面的第一个字符和字符串 B 里面的每一个字符进行比较 if( psz...

...两个字符串,输出第一个字符串在第二个字符串中的位置
答:include <stdio.h>int main(){ char str1[100],str2[100]; char *p,*p1,*p2; gets(str1); gets(str2); for(p=str1;*p!='\0';p++) { for(p1=p;p2=str2;*p1==*p2;p1++,p2++); if(*p2=='\0') { printf("%d",p-str1); return 0...