I have to buy if I am talking about the same person in both variables but in one it comes first with the last names and then the names and in the other the first names and then the last names.
The way I can think of to validate this is that both sentences have exactly the same characters and the same amount of them, but how do I do that?
String var1 = "Mauricio de Jesús Montes de la Oca";
String var2 = "Montes de la Oca Mauricio de Jesús";
//imprime true
String var1 = "Rodrigo Hernández";
String var2 = "Hernández Rodrigo";
//imprime true
String var1 = "Rodolfo Godínez Narrado";
String var2 = "Godínez Rodolfo";
//imprime false
String var1 = "María Magdalena Romero";
String var2 = "Romero María Magdalena";
//imprime true
How do I validate that both are the same? Or at least that they meet the requirement of having exactly the same characters and the same number of words?
What is done here is through the split() function to separate both names by their spaces and the words that are obtained are stored in an array. Subsequently, both arrays are ordered, and it is asked if the size of both are equal, if so, the arrays are traversed and in the variables n1 and n2 each element of arr1 and arr2 are concatenated respectively, after the cycle is exited, it is asked if n1 is equal to n2, if so, the equal variable takes the value of true, then it asks if the equal variable is true, if it is true, it prints that the names are equal, if not, it prints that they are not equal