Breaking

Sunday, 17 September 2017

Three Strings

                                                           Three Strings


Given N string values, the program must print 3 string values as the output as described in the Example Input/Output section.
Input format:
The first line will contain N denoting the number of string values.
Next N lines will contain the N string values.
Output format:
Three lines containing string values as described in the Example Input/Output section.
Example Input/Output 1:
Input:
3
JOHN
JOHNY
JANARDHAN
Output:
JJOJAN
OHHARD
NNYHAN
Example Input/Output 2:
Input:
4
JOHN
JOHNY
JANARDHAN
MIKESPENCER
Output:
JJOJANMIKE
OHHARDSPE
NNYHANNCER

Code:


import java.util.*;
public class Hello {

    public static void main(String[] args) {
                   Scanner sc=new Scanner(System.in);
                   int n=sc.nextInt();
                   sc.nextLine();
                   int i;
                   String str[]=new String[n];
                   for(i=0;i<n;i++)
                       str[i]=sc.nextLine();
                      String p1="",p2="",p3="",ot1="",ot2="",ot3="";
                      int ctr=0;
                    for(i=0;i<n;i++)
                    {
                        int l=str[i].length();
                        ctr++;
                        int ct=l-ctr;
                        p1=str[i].substring(0,ctr);
                        p2=str[i].substring(ctr,ct);
                        p3=str[i].substring(ct,l);
                       ot1+=p1;
                       ot2+=p2;
                       ot3+=p3;
                    }
                   System.out.println(ot1);
                   System.out.println(ot2);
                   System.out.println(ot3);
          }

}

Please do comment If u have any Queries!

7 comments:

  1. import java.util.*;
    public class Hello
    {
    public static void main(String[] args)
    {
    Scanner amr=new Scanner (System.in);
    int n=amr.nextInt();
    amr.nextLine();
    String str[]=new String[n];
    for(int i=0;i<n;i++) str[i]=amr.nextLine();
    String p1="";
    String p2="";
    String p3="";
    String ot1="";
    String ot2="";
    String ot3="";
    int ctr=0;
    for(int i=0;i<n;i++)
    {
    int l=str[i].length();
    ctr++;
    int ct=l-ctr;
    p1=str[i].substring(0,ctr);
    p2=str[i].substring(ctr,ct);
    p3=str[i].substring(ct,l);
    ot1+=p1;
    ot2+=p2;
    ot3+=p3;
    }
    System.out.println(ot1);
    System.out.println(ot2);
    System.out.println(ot3);
    }
    }

    ReplyDelete
  2. import java.util.*;
    public class Hello {

    public static void main(String[] args) {
    //Your Code Here
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String input[]=new String[n+1];
    for(int i=0;i<n+1;i++){
    input[i]=sc.nextLine();


    }
    StringBuilder ss=new StringBuilder();
    StringBuilder ss2=new StringBuilder();
    StringBuilder ss3=new StringBuilder();
    String output[]=new String[3];
    int j=0,i;
    for(i=0;i<n+1;i++){

    String sq=input[i].substring(0,j);

    j++;
    ss.append(sq);
    }

    for(i=0;i<n+1;i++){
    j=(input[i].length());
    int start=j-i;
    String sq=input[i].substring(start,j);
    ss2.append(sq);
    }
    int start=1;
    for(i=1;i<n+1;i++){
    j=(input[i].length());
    int end=j-(i+1);


    String sq=input[i].substring(start,end+1);
    ss3.append(sq);
    start++;
    }
    System.out.println(ss);
    System.out.println(ss3);
    System.out.println(ss2);


    }
    }

    ReplyDelete
  3. admin.. i used ur code to solve this problem.but i strucked with an error string array index out of range in the line substring(0,ctr).

    ReplyDelete
  4. Replies
    1. make sure ur size of array should not less than N.

      String str[]=new String[n];

      Delete
    2. tnk u for ur reply.but i used less than n only. i dont know whats wrong with there.

      Delete
  5. #include
    #include
    int main()
    {
    int n,k=1,l=0;
    scanf("%d",&n);
    char ch[n][100],c[100];
    for(int i=0;i<n;i++){
    scanf("%s",ch[i]);
    int a=strlen(ch[i]);
    for(int j=0;j<k;j++){
    printf("%c",ch[i][j]);
    ch[i][j]='*';
    }
    for(int j=a-k;j<a;j++){
    c[l]=ch[i][j];
    ch[i][j]='*';
    l++;
    }
    k++;
    }
    printf("\n");
    for(int i=0;i<n;i++){
    int a=strlen(ch[i]);
    for(int j=0;j<a;j++){
    if(ch[i][j]!='*')
    printf("%c",ch[i][j]);
    }
    }
    printf("\n%s",c);
    }

    ReplyDelete

Like