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

How to insert a string at x position in another string?

本文关键字:字符串 一个 插入 位置 另一个      更新时间:2023-10-16

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

例如:

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 51n"<<
"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 `` 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]='';
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 `` 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]='';
}
int main(){
char str1[50]= "I have apple";
char str2[50] = "an ";
insertString(str1,str2, 8);
std::cout<<str1;
return 0;
}

如果你必须实现void insertString(char str1[ ],char str2[ ],int position)函数,这里没有太多选择。

可能的解决方案之一是将第一个字符串中的字符向右移动,以便为要插入的第二个字符串腾出空间。

这种方法可能如下所示:

void insertString(char str1[], char str2[], int position) {
const int len1 = strlen(str1);
const int len2 = strlen(str2);
// First, shift all the characters from the given position to the right by `len2` positions:
for (int i = 0; i < len1 - position; ++i) {
str1[len1 - i - 1 + len2] = str1[len1 - i - 1];
}
// Then insert the second string in the given position:
for (int i = 0; i < len2; ++i) {
str1[position + i] = str2[i];
}
// Make sure to create a new terminator since the
// previous one got overwritten by the the first loop
str1[len1 + len2 + 1] = 0;
}

现场演示


请注意,这可能不安全!如果str1数组中没有足够的空间来存储另一个strlen(str2)字符,则会导致缓冲区溢出。

更安全的选择是为堆上的新串联字符串分配一个新的缓冲区,但为了你的分配,这应该足够了。

由于您使用的是C++,因此您应该在任何生产代码中完全避免使用此方法,例如,将 char 数组替换为std::string。但是我已经在评论中读到您还不能使用它。

假设你可以使用<string>,那么我会把std::string::substr使用:

#include <string>
std::string originalString = "I have apple"; // original string
std::string insert = " an "; // added space beforehand for the new word to be inserted
int position = 6; // index where the new word would be inserted
int remainingStringSize = originalString.length() - position - 1; // count how many characters remain from the position you're inserting
std::string combinedString = originalString.substr(0, 6) + insert + originalString.substr(position +1, remainingStringSize); // resulting combined string

请注意,您显然需要检查以下内容:

  • 该索引位置在要插入的原始字符串的长度内
  • 剩余 # 个字符大于 0
相关文章: