Monday, September 11, 2017

Isolate Interlaced Strings

Two string values S1 and S2 are interlaced and passed as a single input string S. Given L1 which is the length of S1, print S1 and S2 as the output.

Input Format:
The first line contains S. The second line contains L1 and L2 separated by a space.

Output Format:
The first line contains S1. The second line contains S2.

Boundary Conditions:
4 <= LENGTH(S) <= 100 1 <= LENGTH(S1) <= 99 1 <= LENGTH(S2) <= 99

Example Input/Output 1:
Input:
LBARZIYSK
4
Output:
LAZY BRISK

Solution:
#include <iostream>
using namespace std;
int main()
{
 int i,n;
 string s,x,y;
 cin>>s>>n;
 for(i=0;i<s.length();i++)
 {
     if(x.length()==n)
      y+=s[i];
     else if(y.length()+n==s.length())
      x+=s[i];
     else
      i%2==0?x+=s[i]:y+=s[i];
 }
 cout<<x<<endl<<y;
}
Share: