I have a project in which I have to find character matches between 3 different Strings of 4 characters, for example:
String cadena1 = "aaattkggl"
String cadena2 = "rrattkmmt"
String cadena3 = "wwattkkllg"
As you can see between the 3 strings there are the characters "attk" and the program should print "attk".
if you have more than 4 characters joined in the 3 strings you should print them, example:
String cadena1 = "cadenaxyuiop"
String cadena2 = "qwertcadenaxqwert"
String cadena3 = "asdfghcadenax"
should print: "stringx"
Thanks to another user, I was able to move forward with this code:
//Pedir datos al usuario y validarlos
System.out.print("Ingrese primer cadena: ");
cadena1 = leer.nextLine();
if (cadena1.contains("b")||cadena1.contains("j")||cadena1.contains("ñ")||cadena1.contains("x")||cadena1.contains("z"))
{
System.out.println("La cadena contiene caracteres invalidos");
}
else
{
System.out.println("\n");
System.out.print("Ingrese segunda cadena: ");
cadena2 = leer.nextLine();
if (cadena2.contains("b")||cadena2.contains("j")||cadena2.contains("ñ")||cadena2.contains("x")||cadena2.contains("z"))
{
System.out.println("La cadena contiene caracteres invalidos");
}
else
{
System.out.println("\n");
System.out.print("Ingrese tercer cadena: ");
cadena3 = leer.nextLine();
if (cadena3.contains("b")||cadena3.contains("j")||cadena3.contains("ñ")||cadena3.contains("x")||cadena3.contains("z"))
{
System.out.println("La cadena contiene caracteres invalidos");
}
//dividir la cadena en 4 caracteres y compararlos con las otras cadenas
else
{
System.out.println("\n***************************************************");
for (int i=4; i<cadena1.length();i++)
{
String[] partes = cadena1.split("(?<=\G.{" + i + "})");
for (String parte : partes)
{
if (parte.length() < 4) break;
if(cadena2.contains(parte)&&cadena3.contains(parte))
cadenator.add(parte);
}
}
}
System.out.println(cadenator);
}
}
}
but I can not get the expected result printed with any string, like the one in the first example.
In the same way, you should be able to print several matches if there are any, for example:
String cadena1 = "holaxxxxxxamigo"
String cadena2 = "amigottttttttthola"
String cadena3 = "ggggggggholagamigo"
this should print "hello" - "friend"
If someone could help me, I would appreciate it very much.