Thread: WIPRO CODING TEST PROGRAM

Forum : Java EE FAQ?s and Interview Questions   17-9-2017 11:25:10 PM
User Image
pavani

Newbie

Joined: Jun 28, 2017

Points: 100

Threads: 1

Posts: 0

Answer Icon WIPRO CODING TEST PROGRAM

| Quote Date : Sep 17, 2017    Views:6495    

Please write a program by using the following conditions

Question:
Encoding Three Strings: Anand was assigned the task of coming up with an encoding mechanism for any given 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 FRONT,MIDDLE And END parts respectively.
"johny" should be split into "jo","h","ny" as FRONT,MIDDLE And END parts respectively.
"janardhan" should be split into "jan","ard","han" as 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 part will get the equal characters,as seen in the example of "johny".

STEP TWO:
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 input 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 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", 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 appliying 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 program that would do the above mentioned processing on any given three strings and generate the resulting three output strings.
The given function's three parameters,input1,input2 and input3 represent the three given input strings.The function should do the processing on these three input strings and return a "Result" object/struct with the three output strings assigned within it.

Comments

User Image
Abhishek3

Newbie

Joined : Jun 25, 2017

Points : 140

Threads: 7

Posts: 26

Answer IconRe: WIPRO CODING TEST PROGRAM

Reply Date : Sep 18, 2017


package com.srimanjavagroup;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter first input String");
String input1=sc.next();
System.out.print("Enter second input String");
String input2=sc.next();
System.out.print("Enter third input String");
String input3=sc.next();
String[] str = divideString(input1);
String[] str2 = divideString(input2);
String[] str3 = divideString(input3);
String in1 = str[0] + str2[1] + str3[2];
String in2 = str[1] + str2[2] + str3[0];
String in3 = str[2] + str2[0] + str3[1];
String fstr1=toggleString(in1);
String fstr2=toggleString(in2);
String fstr3=toggleString(in3);
System.out.println(fstr1);
System.out.println(fstr2);
System.out.println(fstr3);
}

public static String[] divideString(String input) {
int rem = input.length() % 3;
int div = input.length() / 3;
String first = null;
String middle = null;
String last = null;
if (rem == 1) {
first = input.substring(0, div);
middle = input.substring(first.length(), first.length() + div + rem);
last = input.substring(first.length() + middle.length(), input.length());
}

if (rem == 2) {
first = input.substring(0, div + 1);
middle = input.substring(first.length(), first.length() + div);
last = input.substring(first.length() + middle.length(), input.length());
}
if (rem == 0) {
first = input.substring(0, div);
middle = input.substring(first.length(), first.length() + div);
last = input.substring(first.length() + middle.length(), input.length());

}
return new String[] { first, middle, last };
}

public static String toggleString(String str) {
char[] ch = str.toCharArray();
String s="";
for (int i = 0; i < ch.length; i++) {
if (Character.isLowerCase(ch[i])) {
s=s+Character.toUpperCase(ch[i]);
}
if (Character.isUpperCase(ch[i])) {
s=s+Character.toLowerCase(ch[i]);
}
}
return s;
}
}
?

Load More

No More Comments