One In Another

In this program you are given with two strings, you need to find character of string2 in string1, once it is find then get index of that character and return the same else returns zero.
import java.util.*;
public class HackerRank {
public static int oneInAnother(String input1,String input2)
{
int index=0;
String [] input1Array=new String [input1.length()];
String [] input2Array=new String [input2.length()];
input1Array=input1.split(""); //converting string to string Array
input2Array=input2.split(""); //converting string to string Array
for(int i=0;i<input2Array.length;i++){
String a=String.valueOf(input2Array[i]);
for(int j=0;j<input1Array.length;j++){
String b=String.valueOf(input1Array[j]);
if(a.equals(b)){
index=j;
break;
}
}
}
return index;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int output = 0;
String ip1 = in.nextLine().trim();
String ip2 = in.nextLine().trim();
output = oneInAnother(ip1,ip2);
System.out.println(String.valueOf(output));
in.close();
}
}
view raw OneInAnother hosted with ❤ by GitHub




Input: 
adf6ysh
123456

output: 3

Input: 
adf6ysh
123455

output: 0

Comments

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series