Logical Building 03 09 1 PDF
Logical Building 03 09 1 PDF
Logical Building 03 09 1 PDF
Anand was assigned the task of completing up with an encoding mechanism for any given three
strings. He has come up with the below plan.
STEP ONE: Given any three strings, break each string into 3 parts each.
if the no. of characters in the string are in multiples of 3, then each split-part will contain
equal no. Of characters.
if the no. of characters in the string are NOT in multiples of 3, and if these is one
character more than multiple of 3, then the MIDDLE part will get the extra character.
if the no. of characters in the string are NOT in multiples of 3, and if these is two
character more than multiple of 3, then the FIRST and END part will get the extra character
each
STEP TWO: Concatenate (join) the FRONT, MIDDLE and END parts of the strings as per the
below specified concatenation-rule to form three Output strings,
Output1 = FRONT part of input1 + MIDDLE part of input2 + END part of input3
Output2 = MIDDLE part of input1 + END part of input2 + FRONT part of input3
Output3 = END part of input1 + FRONT part of input2 + MIDDLE part of input3
STEP THREE: Process the resulting output strings based on the output processing rule. After the
above two steps, we will now have three output strings, further processing is required only for
the third output string as per below rule – “Toggle the case of each character in the string”.
Anand approaches you to help write a program that would do the above mentioned processing
on any given three strings and generate the resulting three output strings.
Input Format
The first line of input contains string input1
The second line of input contains string input2
The third line of input contains string input3
Constraints
1. The length of the strings is limited from 1 to 50
Output Format
The encoded strings in the given format should be assigned inside output1, output2 and
output3 respectively.
Sample Input 0
John
Johny
Janardhan
Sample Output 0
Jhhan ohnyJan NjOARD
Explanation 0
Step 1: For example – if the three strings are as below –
Input1 = “John”
Input2 = “Johny”
Input3 = “Janardhan”
“John” should be split into “J”, “oh”, “n” as the FRONT, MIDDLE, and END parts respectively.
“Johny” should be split into “Jo”, “h”, “ny” as the FRONT, MIDDLE, and END parts respectively.
“Janardhan” should be split into “Jan”, “ard”, “han” as the FRONT, MIDDLE, and END parts
respectively.
Step 2: For example, for the above specified example input strings,
Output1 = “J” + “h” + “han” = “Jhhan”
Output2 = “oh” + “ny” + “Jan” = “ohnyJan”
Output3 = “n” + “Jo” + “ard” = “nJoard”
Step 3: For example, for the above example strings, Output3 is “nJoard”, so after applying the
toggle rule, Output3 should become “NjOARD”
Final Result – the three output string after applying the above three step is the final result. i.e.
for the above example,
Output1 = “Jhhan”
Output2 = “ohnyJan”
Output3 = “NjOARD”
Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
output1="";
output2="";
output3="";
int len=input1.length();
String f1="",m1="",l1="";
if(len%3==1)
{
int a=len/3;
f1=input1.substring(0,a);
m1=input1.substring(a,2*a+1);
l1=input1.substring(2*a+1,len);
}
else if(len%3==2)
{
int a=len/3;
f1=input1.substring(0,a+1);
m1=input1.substring(a+1,2*a+1);
l1=input1.substring(2*a+1,len);
}
else
{
int a=len/3;
f1=input1.substring(0,a);
m1=input1.substring(a,2*a);
l1=input1.substring(2*a,len);
}
int len1=input2.length();
String f2="",m2="",l2="";
if(len1%3==1)
{
int a=len1/3;
f2=input2.substring(0,a);
m2=input2.substring(a,2*a+1);
l2=input2.substring(2*a+1,len1);
}
else if(len1%3==2)
{
int a=len1/3;
f2=input2.substring(0,a+1);
m2=input2.substring(a+1,2*a+1);
l2=input2.substring(2*a+1,len1);
}
else
{
int a=len1/3;
f2=input2.substring(0,a);
m2=input2.substring(a,2*a);
l2=input2.substring(2*a,len1);
}
int len2=input3.length();
String f3="",m3="",l3="";
if(len2%3==1)
{
int a=len2/3;
f3=input3.substring(0,a);
m3=input3.substring(a,2*a+1);
l3=input3.substring(2*a+1,len2);
}
else if(len2%3==2)
{
int a=len2/3;
f3=input3.substring(0,a+1);
m3=input3.substring(a+1,2*a+1);
l3=input3.substring(2*a+1,len2);
}
else
{
int a=len2/3;
f3=input3.substring(0,a);
m3=input3.substring(a,2*a);
l3=input3.substring(2*a,len2);
}
output1+=f1.concat(m2).concat(l3);
output2+=m1.concat(l2).concat(f3);
String temp=l1.concat(f2).concat(m3);
for(int i=0;i<temp.length();i++)
{
if(Character.isUpperCase(temp.charAt(i)))
{
output3+=Character.toLowerCase(temp.charAt(i));
}
else
{
output3+=Character.toUpperCase(temp.charAt(i));
}
}
String input1=in.nextLine();
String input2=in.nextLine();
String input3=in.nextLine();
}
LB_String_UserIDGen
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_Name. Last_Name. PIN 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
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 0
Rajiv
Roy
560037
6
Sample Output 0
rrOY75
Explanation 0
Stepl - Length of Last_Name Is less than the Length of First_Name, so the Smaller Name is
"Roy" and the Longer Name is "Rajiv"
Step2 - First Letter of "Rajiv" + Entire word of "Roy" + 6th Digit of PIN from left + 6th Digit of PIN
from right = R + Roy + 7 + 5 Therefore, user-id = RRoy75
Step3 - Toggle the alphabets in the user-id So user-id = rrOY75
Sample Input 1
Manoj
Kumar
561327
2
Sample Output 1
mkUMAR62
Explanation 1
Stepl - Length of First_Name is equal to the Length of Last_Name. Alphabetically. 'Kumar’
appears earlier than 'Manoj’. So the Smaller Name is 'Kumar' and the Longer Name is 'Manoj'
Step2 - First Letter of Manoj + Entire word of Kumar + 2nd Digit of PIN from left + 2 nd Digit of
PIN from right = M + Kumar + 6 + 2 Therefore. user-id = MKumar62
Step3 - Toggle the alphabets in the user-id So user-id = mkUMAR62
Sample Input 2
Kumud
Kumar
561327
2
Sample Output 2
kkUMAR62
Explanation 2
Stepl - Length of First_Name is equal to the Length of Last_Name. Alphabetically. 'Kumar
appears earlier than 'Kumud'. So the Smaller Name is "Kumar" and the Longer Name is
"Kumud"
Step2 - First Letter of "Kumud" + Entire word in "Kumar" + 2nd Digit of PIN from left + 2nd Digit
of PIN from right = K+Kumar+6+2 Therefore. user-id = KKumar62
Step3 - Toggle the alphabets in the user-id So user-id = kkUMAR62
Sample Input 3
Arul
Arun
411532
2
Sample Output 3
aaRUL13
Sample Input 4
rAMESH
suresh
600000
5
Sample Output 4
SRamesh00
Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int len1=input1.length();
int len2=input2.length();
String s="",l="",r="";
if(len1>len2)
{
l=input1;
s=input2;
}
else if(len2>len1)
{
l=input2;
s=input1;
}
else
{
for(int i=0;i<len1;i++)
{
int v1=(int)input1.charAt(i);
int v2=(int)input2.charAt(i);
if(v1>v2)
{
l=input1;
s=input2;
break;
}
else if(v2>v1)
{
l=input2;
s=input1;
break;
}
}
String fl=Character.toString(l.charAt(0));
String sl=s;
String pin=Integer.toString(input3);
String p1=Character.toString(pin.charAt(input4-1));
String rev=br.reverse().toString();
String p2=Character.toString(rev.charAt(input4-1));
String fin=fl.concat(sl).concat(p1).concat(p2);
String b="";
for(int i=0;i<fin.length();i++)
{
if(Character.isLowerCase(fin.charAt(i)))
{
b+=Character.toUpperCase(fin.charAt(i));
}
else if(Character.isUpperCase(fin.charAt(i)))
{
b+=Character.toLowerCase(fin.charAt(i));
}
else
{
b+=fin.charAt(i);
}
}
output1=b;
}
String input1=in.next();
String input2=in.next();
int input3=in.nextInt();
int input4=in.nextInt();
System.out.println(output1);
}