+ 4
Question
write a program to read two strings and form third array with each character of the first followed by the second in the third array. for exanple A[]=abcd B[]=xyz output is C[]="axbyczd"
1 Answer
+ 2
Sound more like a demand than a question. lol :D
Anyways, if it's a question, you can do that with a simple loop.
https://code.sololearn.com/ccK95UB9NL0u/#cpp
int main() {
string firstString[1] = "abcd";
string secondString[1] = "xyz";
string thirdString[1] = "";
for(int i = 0; i < firstString[0].length(); ++i) {
thirdString[0] += firstString[0].at(i);
if(i < secondString[0].length()) {
thirdString[0] += secondString[0].at(i);
}
}
cout << thirdString[0];
return 0;
}
However, I did it like that because it's what you asked. Using arrays in this scenario doesn't make much sense since you're only utilizing 1 element in all of them. You can easily do this with just simple string variables and not arrays.
https://code.sololearn.com/cdV1ExjyaS5Z/#cpp
#include <iostream>
using namespace std;
#include <string>
int main() {
string firstString = "abcd";
string secondString = "xyz";
string thirdString = "";
for(int i = 0; i < firstString.length(); ++i) {
thirdString += firstString.at(i);
if(i < secondString.length()) {
thirdString += secondString.at(i);
}
}
cout << thirdString;
return 0;
}