how to compare the letters of a String that are written of distinctly

1

hi what basically is that we can compare one string with another to see that they are the same example

String n1 = "hola";
String n2 = "hola";

we could use:

n1.equals(n2)

but my case is that I am doing an exercise of triploid words which when repeated several times forms another example "nun" if you say fast "nun" several times you would hear "jamon" I have an array wordTrifelias in which the user has entered the number of cases a case is a pair of words this pair is stored in each position for it to the number of cases it is divided by 2 to store the pair of words trifelias it is worth mentioning that a word triplet (a) can not be the same word for example if the first time the user entered say "paco" "paco" this in the output would give a NO since to be trifle should enter "paco" "cup"

palabra[0] = "monja"
palabra[1] = "jamon"

What I would like is to compare these two words but I know that with .equals () I could not because the strings are different but they have the same letters so I compare both characters to know that both have the same letter since in the output I need to return YES or No

For example, I have advanced only this: the for each that is only put in place to prove that it is stored in the positions that I wanted

but according to the problem something should be left

    
asked by Vladimir Joel 03.05.2018 в 06:01
source

4 answers

4

For what you ask, that different cases of the same run can be evaluated, the following would be done:

Let's say the following case:

    public String orderChars(String _str)
    {
        char[] arrStr = _str.toCharArray();
        Arrays.sort(arrStr);
        StringBuilder str = new StringBuilder();
        int i;
        for (i = 0; i < arrStr.length; i++)
            str.append(arrStr[i]);

        return str.toString();
    }

    int casos = 4;
    String palabrasTrifelias[] = {"monja", "jamon", "copa" ,"paco" ,"pavio" ,"viona", "masa", "mesa"};

    Boolean resultado[] = new Boolean[casos];  //Aquí vamos a almacenar si están bien o no. Por defecto se inicializa todo a false, lo tenemos que pasar todo a true para la comparación que haremos luego.

    for(int i=0; i<casos; i++)
    {
        resultado[i] = true;
    }

    //Vamos mirando todas las palabras de 2 en 2
    for(int i=0; i< palabrasTrifelias.length; i=i+2)
    {
        String p1 = palabrasTrifelias[i];
        p1 = p1.replace("b", "v"); //Sustituimos las posibles b por v
        String p2 = palabrasTrifelias[i+1];
        p2 = p2.replace("b", "v");

        //Ahora ya podemos comparar las palabras de 2 en 2.
        //Primero tienen que medir lo mismo, sino ya no son iguales
        //Tampoco puede ser la misma palabra
        if(p1.length()==p2.length() && !p1.equals(p2))
        {
            p1 = orderChars(p1);  //Ordenamos los caracteres
            p2 = orderChars(p2);  //Ordenamos los caracteres

            for(int j = 0; j<p1.length(); j++)
            {
                if(p1.charAt(j)!=p2.charAt(j)){resultado[i/2]=false;} //Como están ordenados, comparamos 1 a 1 todos los caracteres
            }
        }else{
            resultado[i/2]=false;
        }
    }

//Ahora imprimimos los resultados
    System.out.println("Output: ");
    for(int i=0; i<casos; i++)
    {
        System.out.println(resultado[i]+ " ");
    }
    
answered by 03.05.2018 / 08:12
source
1
  

You could do it this way too, as I mentioned in the comment with charAt ()

public static void main(String[] args) {

    Scanner Entrada = new Scanner(System.in);
    int Casos;

    System.out.print("Ingrese cantidad de casos: ");
    Casos = Entrada.nextInt();
    Casos = Casos*2;

    String PalabrasTrifelias[] = new String[Casos];

    for( int i = 0; i < PalabrasTrifelias.length; i++ ){
        PalabrasTrifelias[i] = Entrada.next();
        if( i % 2 == 0){
            System.out.println();
        }
    }

    int nx = 0;
    for ( String Palabras:PalabrasTrifelias){
        System.out.printf(" %d : %s%n", nx, Palabras);
        nx++;
    }

    nx = 0;
    for ( String Palabras:PalabrasTrifelias){
        String n1 = Palabras;
        nx++;
        String n2 = Palabras;
        int contador = 0;
        // Verifico que tengan el mismo numero de carateres creo que deberia tener el mismo numero
        if( n1.length() == n2.length()){
            // Ciclo para recorrer el string n1
            for(int i = 0; i < n1.length(); i++){
                // Ciclo para recorrer string n2
                for (int j = 0; j < n2.length(); j++){
                    // condicinal para ir comparando
                    if( n1.charAt(i) == n2.charAt(j)){
                        contador++;
                    }
                }
            }
        } 
        if (contador == n1.length()){
            System.out.println("Si cumple"+ n1 + " con "+ n2);
        }else{
            System.out.println("No cumple"+ n1 + " con "+ n2);
        }
        nx++;
    }
} 
    
answered by 03.05.2018 в 07:15
1

Another possible implementation:

public static void main(String[] args) {
    trifelio("monja", "jamon");
    trifelio("Paco", "copa");
    trifelio("carro", "roca");
    trifelio("lavese", "besela");
    trifelio("vota", "bota");
}

private static void trifelio(String str1, String str2) {
    System.out.println("esTrifelio(" + str1 + ", " + str2 + "): " + esTrifelio(str1, str2));
}

private static boolean esTrifelio(String str1, String str2) {
    if(str1 == str2) {
        return false;
    }

    str1 = str1.replace("v", "b").toLowerCase();
    str2 = str2.replace("v", "b").toLowerCase();

    if(str1.equals(str2)) {
        return false;
    }

    return str1.concat(str1).contains(str2) && str2.concat(str2).contains(str1);
}

The idea behind is that if 2 Strings A, B are triplets, then you have to:

  • AA contains B
  • BB contains A
answered by 03.05.2018 в 15:21
0

You can pose it as long as the two words have the same length, of course if one has more letters than the other obviously would no longer be triplet words, what I did was to transform the strings into arrays of characters and compare in this the first position of the first word, that is, the m of a nun with each of the letters of the second word, if it was found, it added one to the variable of integer type called coincidence, at the end of each position of the first word questions if what is worth coinciding is equal to the length of your string, if so, they are triplet words, otherwise, they are not triplet words

String palabra1 = "monja";
String palabra2 = "jamon";
int coincidencia = 0;

char[] Caracteres1 = palabra1.toCharArray();
char[] Caracteres2 = palabra2.toCharArray();

for (int x=0;x<Caracteres1.length;x++){
    for (int y=0;y<Caracteres2.length;y++){
      if(Caracteres1[x] == Caracteres2[y]){
          coincidencia++;
      }
    }
}

if(Caracteres1.length == coincidencia){
    System.out.println("SI");
}
else{
    System.out.println("NO");
}

If your answer was helpful, do not forget to rate it.

    
answered by 03.05.2018 в 07:09