problem no 34:(problem on string): merge two strings.
QUESTIONS:
Given two strings S1 and S2 as input, the task is to merge them alternatively i.e. the first character of S1 then the first character of S2 and so on till the strings end.
NOTE: Add the whole string if other string is empty.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two strings S1 and S2.
Output:
For each test case, in a new line, print the merged string.
Constraints:
1 <= T <= 100
1 <= |S1|, |S2| <= 104
Example:
Input:
2
Hello Bye
abc def
Output:
HBeylelo
adbecf
ANSWER:
#include <iostream>
#include<cstring>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
string s,t;
cin>>s>>t;
string u;
int large;
int small;
if(s.size()>t.size())
{
large=s.size();
small=t.size();
}
else
{
large=t.size();
small=s.size();
}
for(int i=0;i<large;i++)
{
if(i<small)
{
u=u+s[i];
u=u+t[i];
}
else
{
if(s.size()==large)
{
u=u+s[i];
}
else
{
u=u+t[i];
}
}
}
cout<<u<<"\n";
}
return 0;
}
Comments
Post a Comment