100% found this document useful (1 vote)
3K views

4.userid Generation Question and Answers

This document contains code for generating a user ID from input strings and numbers. It takes in two strings, and two integers. It compares the string lengths and characters to determine the smaller and longer string. It then takes characters from the end of the smaller string, the entire longer string, and specific positions of a third string and an integer to generate a user ID string. It then converts each character in the user ID string to uppercase if it was lowercase and vice versa to return the final user ID.

Uploaded by

addssdfa
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3K views

4.userid Generation Question and Answers

This document contains code for generating a user ID from input strings and numbers. It takes in two strings, and two integers. It compares the string lengths and characters to determine the smaller and longer string. It then takes characters from the end of the smaller string, the entire longer string, and specific positions of a third string and an integer to generate a user ID string. It then converts each character in the user ID string to uppercase if it was lowercase and vice versa to return the final user ID.

Uploaded by

addssdfa
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Solution:

import java.io.*;
import java.util.*;

// Read only region start


class UserMainCode
{

public String userIdGeneration(String input1,String input2,int input3,int input4){


// Read only region end
// Write code here...
String small="";
String longer="";
int l1=input1.length(); // length of input1
int l2=input2.length(); // length of input2
int f=0;
if(l1<l2) // step1:if length of input1 is lesser than length of input2
{
small=input1;
longer=input2;
}
else if(l1>l2) // step1:if length of input2 is lesser than length of input1
{
small=input2;
longer=input1;

}
else //step1: if lengths of input1 and input2 are equal
{
for(int i=0;i<l1;i++)
{
if(input1.charAt(i)<input2.charAt(i))
{
small=input1;
longer=input2;
f=1;
break;
}
else if(input2.charAt(i)<input1.charAt(i))
{
small=input2;
longer=input1;
f=1;
break;
}
else
{
continue;
}
}
if(f==0) // if both the strings are equal
{
small=input1;
longer=input2;
}
}
String userid="";
char ar1[]=small.toCharArray(); // convert small string into character array
char ar2[]=longer.toCharArray(); // convert longer string into character array
String s1=String.valueOf(ar1[ar1.length-1]); // step2: get the last char of small and convert it into string
String s2=longer; // step2: get the entire longer string
char ar3[]=String.valueOf(input3).toCharArray(); // convert input3 into string and convert into char
array
String s3=String.valueOf(ar3[input4-1]); // step2: get the nth value from input3 by traversing from left to
right
String s4=String.valueOf(ar3[ar3.length-input4]); // step2: get the nth value from input3
by traversing from right to left
userid=s1+s2+s3+s4; // combine all the strings
String res="";
for(int i=0;i<userid.length();i++) // step3: convert the lowercase character into uppercase and
uppercase charcter into lowercase
{
if(Character.isLowerCase(userid.charAt(i)))
{
res=res+Character.toUpperCase(userid.charAt(i));
}
else
{
res=res+Character.toLowerCase(userid.charAt(i));
}
}
return res;

}
}

You might also like