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

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.