50% found this document useful (2 votes)
21K views

Encoding Three Strings (5 Test Cases)

Anand was tasked with encoding three input strings. His encoding method involves: 1) Splitting each string into three parts: front, middle, end 2) Concatenating the parts to create three output strings: Output1 = middle of input1 + front of input2 + end of input3 Output2 = end of input1 + middle of input2 + front of input3 Output3 = front of input1 + end of input2 + middle of input3 3) Toggling the case of characters in the third output string. The program splits each input string, concatenates the parts as specified, and toggles the third output string's character cases to produce the

Uploaded by

Unknown
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
21K views

Encoding Three Strings (5 Test Cases)

Anand was tasked with encoding three input strings. His encoding method involves: 1) Splitting each string into three parts: front, middle, end 2) Concatenating the parts to create three output strings: Output1 = middle of input1 + front of input2 + end of input3 Output2 = end of input1 + middle of input2 + front of input3 Output3 = front of input1 + end of input2 + middle of input3 3) Toggling the case of characters in the third output string. The program splits each input string, concatenates the parts as specified, and toggles the third output string's character cases to produce the

Uploaded by

Unknown
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

LOGIC 1:

Encoding Three Strings: Anand was assigned the task of coming 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.

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.

i.e. if the no. of characters in the string are in multiples of 3, then each split-part will contain equal no. of
characters, as seen in the example of “Janardhan”

If the no. of characters in the string are NOT in multiples of 3, and if there is one character more than
multiple of 3, then the middle part will get the extra character, as seen in the example of “John”

If the no. of characters in the string are NOT in multiples of 3, and if there are two characters more than
multiple of 3, then the FRONT and END parts will get one extra character each, as seen in the example of
“Johny”

STEP TWO: Concatenate (join) the FORNT, 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

For example, for the above specified example strings,

Output1= “J”+”h”+”han”=”Jhhan”

Output2=”oh”+”ny”+”Jan”=”ohnyJan”

Output3=”n”+”Jo”+”ard”=”nJoard”

STEP THREE: Process the resulting output strings based on the output-processing rule.
After the above, 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”, i.e in the third output string, all lower-case characters
should be made upper-case and vice versa.

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 strings after applying the above three steps is the final result. i.e. for
the above example,

Output1=”Jhhan”

Output2=”ohnyJan”

Output3=”NjOARD”

Anand approaches you to help him write a program that would do the above mentioned processing on
any given three strings and generate the resulting three output strings.

EXAMPLE 1:

Input1=”John”

Input2=”Johny”

Input3=”Janardhan”

Output1=”Jhhan”

Output2=”ohnyJan”

Output3=”NjOARD”

EXAMPLE 2 :

Input1=”Raagam”

Input2=”Talam”

Input3=”Pallavi”

Output1=”Ralvi”

Output2=”agamPa”

Output3=”AMtALLA”

EXAMPLE 3 :
Input1=”indiawingol”

Input2=”vandhemadhara”

Input3=”ponaru”

Output1=”indihemadru”

Output2=”awiharapo”

Output3=”ngolvandna”

EXAMPLE 4 :

Input1=”ram”

Input2=”raj”

Input3=”joy”

Output1=”ray”

Output2=”ajj”

Output3=”mro”

EXAMPLE 5:

Input1=”abcdefghijklmnopqrstuvwxyz”

Input2=”acegikmoqsuwy”

Input3=”krishnan”

Output1=”abcdefghiikmoqnan”

Output2=”jklmnopqsuwykri”

Output3=”rstuvwxyzacegsh”
Program
Class UserMainCode

public Result encodeThreeStrings(String input1,String input2,String input3)

String f1="",f2="",f3="",m1="",m2="",m3="",l1="",l2="",l3="";

String out1="",out2="",out3="";

int d=0;

if(input1.length()%3==0)

d=input1.length()/3;

f1=input1.substring(0,d);

m1=input1.substring(d,2*d);

l1=input1.substring(2*d);

else if(input1.length()%3==1)

d=input1.length()/3;

f1=input1.substring(0,d);

m1=input1.substring(d,2*d+1);

l1=input1.substring((2*d)+1);

}
else if(input1.length()%3==2)

d=input1.length()/3;

f1=input1.substring(0,d+1);

m1=input1.substring(d+1,2*d+1);

l1=input1.substring((2*d)+1);

if(input2.length()%3==0)

d=input2.length()/3;

f2=input2.substring(0,d);

m2=input2.substring(d,2*d);

l2=input2.substring(2*d);

else if(input2.length()%3==1)

d=input2.length()/3;

f2=input2.substring(0,d);

m2=input2.substring(d,2*d+1);

l2=input2.substring((2*d)+1);

else if(input2.length()%3==2)

{
d=input2.length()/3;

f2=input2.substring(0,d+1);

m2=input2.substring(d+1,2*d+1);

l2=input2.substring((2*d)+1);

if(input3.length()%3==0)

d=input3.length()/3;

f3=input3.substring(0,d);

m3=input3.substring(d,2*d);

l3=input3.substring(2*d);

else if(input3.length()%3==1)

d=input3.length()/3;

f3=input3.substring(0,d);

m3=input3.substring(d,2*d+1);

l3=input3.substring((2*d)+1);

else if(input3.length()%3==2)

d=input3.length()/3;
f3=input3.substring(0,d+1);

m3=input3.substring(d+1,2*d+1);

l3=input3.substring((2*d)+1);

out1=f1+l2+m3;

out2=m1+f2+l3;

out3=l1+m2+f3;

String out3togle="";

for(int i=0;i<out3.length();i++)

if(Character.isUpperCase(out3.charAt(i)))

out3togle=out3togle+String.valueOf(Character.toLowerCase(out3.charAt(i)));

else

out3togle=out3togle+String.valueOf(Character.toUpperCase(out3.charAt(i)));

return new Result(out1,out2,out3togle);

}
LOGIC 2:

Encoding Three Strings: Anand was assigned the task of coming 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.

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.

i.e. if the no. of characters in the string are in multiples of 3, then each split-part will contain equal no. of
characters, as seen in the example of “Janardhan”

If the no. of characters in the string are NOT in multiples of 3, and if there is one character more than
multiple of 3, then the middle part will get the extra character, as seen in the example of “John”

If the no. of characters in the string are NOT in multiples of 3, and if there are two characters more than
multiple of 3, then the FRONT and END parts will get one extra character each, as seen in the example of
“Johny”

STEP TWO: Concatenate (join) the FORNT, MIDDLE and END parts of the strings as per the below
specified concatenation-rule to form three Output Strings.

Output1= MIDDLE part of input1+FIRST part of Input2+END part of Input3

Output2= END part of input1+MIDDLE part of Input2+FRONT part of Input3

Output3= FRONT part of input1+END part of Input2+MIDDLE part of Input3

For example, for the above specified example strings,

Output 1 : ohJohan

Output 2: nhJan

Output 3: Jnyard

STEP THREE: Process the resulting output strings based on the output-processing rule.
After the above, 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”, i.e in the third output string, all lower-case characters
should be made upper-case and vice versa.

For example, for the above example strings, Output3 is “Jnyard”, so after applying the toggle rule
Output3 should become “jNYARD”.

FINAL RESULT – The three output strings after applying the above three steps is the final result. i.e. for
the above example,

Output 1 : ohJohan

Output 2: nhJan

Output 3: jNYARD

Anand approaches you to help him write a program that would do the above mentioned processing on
any given three strings and generate the resulting three output strings.

Example 1 :

Input1=”John” “J”, “oh”,”n”

Input2=”Johny” “Jo”, “h”,”ny”

Input3=”Janardhan” “Jan”,”ard”,”han”

Output 1 : ohJohan

Output 2: nhJan

Output 3: jNYL

EXAMPLE 2 :

Input1=”Ra ag am”

Input2=”Ta l am”

Input3=”Pal l avi”

Output1= agTaavi

Output2= amlPal

Output3= rAAML
Program
Class UserMainCode

public Result encodeThreeStrings(String input1,String input2,String input3)

String f1="",f2="",f3="",m1="",m2="",m3="",l1="",l2="",l3="";

String out1="",out2="",out3="";

int d=0;

if(input1.length()%3==0)

d=input1.length/3;

f1=input1.substring(0,d);

m1=input1.substring(d,2*d);

l1=input1.substring(2*d);

else if(input1.length()%3==1)

d=input1.length/3;

f1=input1.substring(0,d);

m1=input1.substring(d,2*d+1);

l1=input1.substring((2*d)+1);

}
else if(input1.length()%3==2)

d=input1.length/3;

f1=input1.substring(0,d+1);

m1=input1.substring(d+1,2*d+1);

l1=input1.substring((2*d)+1);

if(input2.length()%3==0)

d=input2.length/3;

f2=input2.substring(0,d);

m2=input2.substring(d,2*d);

l2=input2.substring(2*d);

else if(input2.length()%3==1)

d=input2.length/3;

f2=input2.substring(0,d);

m2=input2.substring(d,2*d+1);

l2=input2.substring((2*d)+1);

else if(input2.length()%3==2)

{
d=input2.length/3;

f2=input2.substring(0,d+1);

m2=input2.substring(d+1,2*d+1);

l2=input2.substring((2*d)+1);

if(input3.length()%3==0)

d=input3.length/3;

f3=input3.substring(0,d);

m3=input3.substring(d,2*d);

l3=input3.substring(2*d);

else if(input3.length()%3==1)

d=input3.length/3;

f3=input3.substring(0,d);

m3=input3.substring(d,2*d+1);

l3=input3.substring((2*d)+1);

else if(input3.length()%3==2)

d=input3.length/3;
f3=input3.substring(0,d+1);

m3=input3.substring(d+1,2*d+1);

l3=input3.substring((2*d)+1);

out1=m1+f2+l3;

out2=l1+m2+f3;

out3=f1+l2+m3;

String out3togle="";

for(int i=0;i<out3.length();i++)

if(Character.isUpperCase(out3.charAt(i)))

out3togle=out3togle+String.valueOf(Character.toLowerCase(out3.charAt(i)));

else

out3togle=out3togle+String.valueOf(Character.toUpperCase(out3.charAt(i)));

return new Result(out1,out2,out3togle);

}
LOGIC 3:

Encoding Three Strings: Anand was assigned the task of coming 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.

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.

i.e. if the no. of characters in the string are in multiples of 3, then each split-part will contain equal no. of
characters, as seen in the example of “Janardhan”

If the no. of characters in the string are NOT in multiples of 3, and if there is one character more than
multiple of 3, then the middle part will get the extra character, as seen in the example of “John”

If the no. of characters in the string are NOT in multiples of 3, and if there are two characters more than
multiple of 3, then the FRONT and END parts will get one extra character each, as seen in the example of
“Johny”

STEP TWO: Concatenate (join) the FORNT, MIDDLE and END parts of the strings as per the below
specified concatenation-rule to form three Output Strings.

Output1= END part of input1+FIRST part of Input2+MIDDLE part of Input3

Output2= FRONT part of input1+MIDDLE part of Input2+END part of Input3

Output3= MIDDLE part of input1+END part of Input2+FRONT part of Input3

For example, for the above specified example strings,

Output 1 : nJoard

Output 2: Jhhan

Output 3: ohnyJan
STEP THREE: Process the resulting output strings based on the output-processing rule.

After the above, 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”, i.e in the third output string, all lower-case characters
should be made upper-case and vice versa.

For example, for the above example strings, Output3 is “ohnyJan”, so after applying the toggle rule
Output3 should become “OHNYjAN”.

FINAL RESULT – The three output strings after applying the above three steps is the final result. i.e. for
the above example,

Output 1 : nJoard

Output 2: Jhhan

Output 3: OHNYjAN

Anand approaches you to help him write a program that would do the above mentioned processing on
any given three strings and generate the resulting three output strings.

EXAMPLE 1:

Input1=”John” “J”, “oh”,”n”

Input2=”Johny” “Jo”, “h”,”ny”

Input3=”Janardhan” “Jan”,”ard”,”han”

Output 1 : nJoard

Output 2: Jhhan

Output 3: OHNYjAN

EXAMPLE 2 :

Input1=”Ra ag am”

Input2=”Ta l am”

Input3=”Pal l avi”

Output1= amTal

Output2= Ralavi Output3= AGAMpAL


Program
Class UserMainCode

public Result encodeThreeStrings(String input1,String input2,String input3)

String f1="",f2="",f3="",m1="",m2="",m3="",l1="",l2="",l3="";

String out1="",out2="",out3="";

int d=0;

if(input1.length()%3==0)

d=input1.length/3;

f1=input1.substring(0,d);

m1=input1.substring(d,2*d);

l1=input1.substring(2*d);

else if(input1.length()%3==1)

d=input1.length/3;

f1=input1.substring(0,d);

m1=input1.substring(d,2*d+1);

l1=input1.substring((2*d)+1);
}

else if(input1.length()%3==2)

d=input1.length/3;

f1=input1.substring(0,d+1);

m1=input1.substring(d+1,2*d+1);

l1=input1.substring((2*d)+1);

if(input2.length()%3==0)

d=input2.length/3;

f2=input2.substring(0,d);

m2=input2.substring(d,2*d);

l2=input2.substring(2*d);

else if(input2.length()%3==1)

d=input2.length/3;

f2=input2.substring(0,d);

m2=input2.substring(d,2*d+1);

l2=input2.substring((2*d)+1);

else if(input2.length()%3==2)
{

d=input2.length/3;

f2=input2.substring(0,d+1);

m2=input2.substring(d+1,2*d+1);

l2=input2.substring((2*d)+1);

if(input3.length()%3==0)

d=input3.length/3;

f3=input3.substring(0,d);

m3=input3.substring(d,2*d);

l3=input3.substring(2*d);

else if(input3.length()%3==1)

d=input3.length/3;

f3=input3.substring(0,d);

m3=input3.substring(d,2*d+1);

l3=input3.substring((2*d)+1);

else if(input3.length()%3==2)

{
d=input3.length/3;

f3=input3.substring(0,d+1);

m3=input3.substring(d+1,2*d+1);

l3=input3.substring((2*d)+1);

out1=l1+f2+m3;

out2=f1+m2+l3;

out3=m1+l2+f3;

String out3togle="";

for(int i=0;i<out3.length();i++)

if(Character.isUpperCase(out3.charAt(i)))

out3togle=out3togle+String.valueOf(Character.toLowerCase(out3.charAt(i)));

else

out3togle=out3togle+String.valueOf(Character.toUpperCase(out3.charAt(i)));

return new Result(out1,out2,out3togle);

You might also like