Breaking

Sunday 10 September 2017

Isolate Interlaced Strings

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
Code:


import java.util.*;
public class Hello {

    public static void main(String[] args) {
                   Scanner sc=new Scanner(System.in);
                   String str=sc.next(),s1="",s2="";
                   int n=sc.nextInt();
                   int k=0,i,l=0,x=0;
              int l2=str.length()-n;
             
                   for(i=0;i<str.length();i++)
                   {
                    if(i%2==0&&l<n)
                      {
                          s1+=str.charAt(i);
                          l++;
                      }
                    else if(x<l2) 
                      { s2+=str.charAt(i);
                       x++;
                      }
                    else
                       {s1+=str.charAt(i);l++;}
                    
                   }
         
          System.out.println(s1);
          System.out.print(s2);

          }

}


Please do comment If u have any Queries!

6 comments:

  1. This code works well!!!!!

    int main()
    {
    char str[100];
    scanf("%s",str);
    int n=strlen(str),i,j=0,k=0,l1,l2;
    scanf("%d",&l1);
    l2=n-l1;
    char a[l1],b[l2];
    for(i=0;i<n;i++){
    if(i%2==0&&j<l1){
    a[j]=str[i];
    j++;
    }
    else if(k<l2){
    b[k]=str[i];
    k++;
    }
    else{
    a[j]=str[i];
    j++;
    }
    }
    for(i=0;i<l1;i++)
    printf("%c",a[i]);
    printf("\n");
    for(i=0;i<l2;i++)
    printf("%c",b[i]);
    }

    ReplyDelete
  2. you don't need the extra else condition just first 2 conditions will be enough the code will be working fine

    ReplyDelete
    Replies
    1. corner cases will fail if we ignore those conditions!!

      Delete
  3. What are the corner conditions ji?

    ReplyDelete
  4. Is there anything wrong in this code? Please give any test cases that does not work in this code...Thank You

    #include
    #include
    #include

    int main()
    {
    int n,l;
    char a[100];

    fgets(a,100,stdin);
    n = strlen(a);
    if(n<4||n>100)
    {
    return 0;
    }

    scanf("%d",&l);
    if(l<1||l>99)
    {
    return 0;
    }

    for(int i=0;i<=(l*2)-2;)
    {
    printf("%c",a[i]);
    i = i + 2;
    }

    printf("\n");

    for(int i=1;i<=(l*2)-1;)
    {
    printf("%c",a[i]);
    i = i + 2;
    }

    for(int i=l*2;i<n;i++)
    {
    printf("%c",a[i]);
    }

    return 0;
    }

    ReplyDelete

Like