I need to append the value of string2 to string1 without using append method in string library. So far, I've only managed to do this:
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1="Messi ";
string s2="best";
int i=s1.size();
int j=s2.size();
int count;
for(count=0;count<=j;count++){
s1[i++]=s2[count];
}
cout<<s1;
}
But this, only gives me the value of s1 which is "Messi ". However, I checked by doing this: s1[8]. Since, s1 has index 5, s1[8] shouldn't work, right? But, it does and I get "s" which is in s2. So, it means that the value of s2 is indeed going in s1 but somehow, typing s1 only shows "Messi".
The reason I ask this because we were given this set of instructions to implement the program(yes it appears to be used on c strings but we were told to implement on string object):
Step 1: Initialize i= strlen(s1)
Step 2: Initialize j=strlen(s2)
Step 3: Initialize count=0
Step 4: Repeat steps 5 to 7 while count<=j
Step 5: s1[i]=s2[count]
Step 6: i=i+1
Step 7: count=count+1