c++ Language
Fix this code to have it concatenate two strings st1 and st2 and put the result in st3 with a space separator. it has to be done using array representation of strings
#include <iostream>
using namespace std;
#include <string>
int main()
{
string st1,st2, st3;
int c = 0, i =0;
cout << "Enter a string: ";
cin >> st1;
cout << "Enter another string: ";
cin >> st2;
while (st1[c] != '\0'){
st3[c] = st1[c];
st3[c+1] = '\0';
cout << endl << st1[c] << " " <<
st3[c];
cout << endl << "current valuse of st3 is: " <<
st3;
c++;
}
st3[c] = ' ';
c++;
while (st2[i] != '\0'){
st3[c] = st2[i];
c++;
i++;
}
st3[c] = '\0';
cout << st3;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Code:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Code:
#include <iostream>
using namespace std;
#include <string>
int main()
{
string st1,st2, st3;
int c = 0, i =0,e = 0;
cout << "Enter a string: ";
cin >> st1;
cout << "Enter another string: ";
cin >> st2;
while (st1[c] != '\0'){
st3[c] = st1[c];
cout << endl << st1[c] << " " <<
st3[c]<<"\n";
c++;
}
st3[c+1] = '\0';
st3[c] = ' ';
c++;
while (st2[i] != '\0'){
st3[c] = st2[i];
c++;
i++;
}
st3[c] = '\0';
while(st3[e]!='\0'){
cout<<st3[e];
e++;
}
cout<<"\n";
return 0;
}
Screenshot of code:
Screenshot of output:
Get Answers For Free
Most questions answered within 1 hours.