gpt4 book ai didi

c++ - 如何在另一个字符串的x位置插入一个字符串?

转载 作者:行者123 更新时间:2023-12-02 09:50:53 27 4
gpt4 key购买 nike

我必须使用一个接受两个字符串和一个整数值的函数,该值指示在第一个字符串中输入第二个字符串的位置。

例如:

String 1 = "I have apple"
String 2 = "an "
position = 6,

输出:
String 1 = "I have an apple"

到目前为止,这是我的代码:
#include <iostream>
using namespace std;

const int SIZE=102;

void insertString(char str1[ ],char str2[ ],int position);

int main()
{
char str1[SIZE], str2[SIZE];
int position,i=0,j=0;

cout<<"Enter string 1 of atmost 50 characters:\n";
cin.getline(str1,SIZE/2);

cout<<"Enter string 2 of atmost 50 characters:\n";
cin.getline(str2,SIZE/2);

cout<<"Enter Position number where String 2 is to be inserted: ";
cin>>position;

while(position<0||position>50)
{
cout<<"Invalid input. Enter a positive Position number less than 51\n"<<
"where String 2 is to be inserted: ";
cin>>position;
}

insertString(str1,str2,position);

cout<<"Modified string 1: "<<str1<<endl;

system("pause");
return 0;
}

/******************************************************************************
Definition of function insertString:

This function takes two C-string in form of character arrays and one integer value
as parameters

It inserts String 2 in String 1 on the required position.

*******************************************************************************/

void insertString(char str1[ ],char str2[ ],int position)
{
char temp[SIZE];
int i,j,countStr2;

for(j=0;j<SIZE&&str2[j]!=0;j++)
countStr2=j;

for(i=position,j=0;i<SIZE,j<=countStr2; i++,j++)
{
temp[i]=str1[i];
str1[i]=str2[j];

}

}

此逻辑将覆盖字符串1的某些字符。我该怎么办?

任何帮助将不胜感激。

最佳答案

#include<cstring>
#include <iostream>
/*The following function supposes that str1 and str2 are cString (i.e)
ended by `\0` and inserts str2 in str1 at the specified position in a
newStr, then returns the new string as apointer*/

char* insertString(const char str1[ ],const char str2[ ],const int& position)
{
int shiftedPos=position-1;
int str2Size=strlen(str2);
int str1Size=strlen(str1);
int newSize=str2Size+str1Size+1;

char* newStr=new char[newSize];
for(int i=0; i<shiftedPos; i++) newStr[i]=str1[i];

for(int i=0; i<str2Size; i++) newStr[shiftedPos+i]=str2[i];
for(int i=0; i<newSize; i++) newStr[shiftedPos+str2Size+i]=str1[shiftedPos+i];

newStr[newSize]='\0';

return newStr;
}
int main(){
auto str1 = "I have apple";
auto str2 = "an ";
std::cout<<insertString(str1,str2, 8);
return 0;
}

另一个代码可以完全按照您的要求执行
#include<iostream>
#include<cstring>
/*The following function supposes that str1 and str2 are cString (i.e)
ended by `\0` and that the memory allocated by str1 >= the len(str1)+len(str2)+1.
It inserts str2 in str1 at the specified position in str1*/

void insertString(char str1[ ], char str2[ ], int position)
{
int str1Size=strlen(str1);
int str2Size=strlen(str2);
int newSize=str2Size+str1Size+1;
int cppPosition=position-1;

for(int i=str1Size; i>=cppPosition; i--) str1[i+str2Size]=str1[i];
for(int i=0; i<str2Size; i++) str1[cppPosition+i]=str2[i];
str1[newSize]='\0';
}
int main(){
char str1[50]= "I have apple";
char str2[50] = "an ";
insertString(str1,str2, 8);
std::cout<<str1;
return 0;
}

关于c++ - 如何在另一个字符串的x位置插入一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59581825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com