Thursday, 15 June 2017

LB_String_UserIDGen

Problem:
Joseph's team has been assigned the task of creating user-ids for all participants of an online gaming competition. Joseph has designed a process for generating the user-id using the participant's First_NameLast_NamePIN code and a number N. The process defined by Joseph is as below:
Step 1 - Compare the lengths of First_Name and Last_Name of the participant.
  • The one that is shorter will be called Smaller Name and the one that is longer will be called the Longer Name.
  • If both First_Name and Last_Name are of equal Length then the name that appears earlier in alphabetical order will be called Smaller Name and the name that appears later in alphabetical order will be called the Longer Name
Step2 - The user-id should be generated as below:
First Letter of the longer name + Entire word of the smaller name + Digit at position N in the PIN when traversing the PIN from left to right + Digit at position N in the PIN when traversing the PIN from right to left.
Step3 - Toggle the alphabets of the user-id generated in step-2 i.e. uppercase alphabets should become lowercase and lowercase alphabets should become uppercase.
Input Format
  • The first line contains the first name (input1).
  • The second line contains the last name (input2).
  • The third line contains the PIN (input3).
  • The fourth line contains the integer N (input4).
Constraints
  1. The value of N (input4) will always be less than or equal to the number of digits in the PIN (input3)
Output Format
  • The method (function) should do the processing as per rules explained above and should assign the generated user-id to the member variable outputl.
Sample Input:

Manoj
Kumar
561327

2

Sample Output:

mkUMAR62

Coding:

public class Solution {

   private static String output1;

   public static void userIdGeneration(String input1, String input2, int input3, int input4) {
   String longer="";
String smaller="";
int s1=input1.length();
int s2=input2.length();
output1="";

if(s1==s2)
{
if(input1.compareTo(input2)>0)
{
longer=input1;
smaller=input2;
}
else
{
longer=input2;
smaller=input1;
}
}else if(s1>s2){
longer=input1;
smaller=input2;
}
else
{
longer=input2;
smaller=input1;
}
String pin=input3+"";
String output=longer.charAt(0)+smaller+pin.charAt(input4-1)+pin.charAt(pin.length()-input4);
for(int i=0;i<output.length();i++)
{
if(Character.isUpperCase(output.charAt(i)))
{
output1+=Character.toLowerCase(output.charAt(i));
}
else if(Character.isLowerCase(output.charAt(i)))
{
output1+=Character.toUpperCase(output.charAt(i));
}
else
{
output1+=output.charAt(i);
}
}

    }
   public static void main(String[] args) {
        /* Do NOT Alter main() */
     Scanner in=new Scanner(System.in);
     String input1=in.next();
     String input2=in.next();
     int input3=in.nextInt();
     int input4=in.nextInt();
     userIdGeneration(input1, input2, input3, input4);
     System.out.println(output1);
    }
}


Output: