Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
El código de descifrado no cambia los datos reales

Tengo problemas para ejecutar mi código correctamente. Creé un método de descifrado que se supone que toma una palabra y reemplaza cada 2 letras entre sí. A menos que la palabra sea una cantidad impar, entonces debo dejar la última letra en paz. El problema es que solo imprimo las letras y no cambio los datos reales de la cadena.

 static String ezDecrypt (String ezEncrypt){ //this variable holds the value of the String's length int cl = ezEncrypt() ; //if the length of the String is even then do this if (ezEncrypt.length() % 2 == 0){ //for loop that begins at 0 //keeps looping until it reaches the end of the string //each loop adds 2 to the loop for(int i = 0; i < cl; i= i + 2) { //will print out the second letter in the string System.out.print(ezEncrypt.charAt(i + 1)); //will print out the first letter in the string System.out.print(ezEncrypt.charAt(i)); } } //if the length of the word is an odd number, then else if(ezEncrypt.length() % 2 != 0){ //loop through and do the same process as above //except leave this loop will skip the last letter for(int i = 0; i < cl-1; i= i + 2) { //will print out the second letter in the string System.out.print(ezEncrypt.charAt(i + 1)); //will print out the first letter in the string System.out.print(ezEncrypt.charAt(i)); } } return ezEncrypt; }
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Entiendo que está tratando de modificar una cadena para descifrarla. Bueno, tengo algunas noticias para ti: la clase String en Java ha sido diseñada de tal manera que los objetos String son inmutables. Eso significa que no puede modificar sus contenidos una vez que los haya creado. Pero no te preocupes, hay otras formas de lograr lo que tienes en mente.

Por ejemplo, puede obtener una matriz de caracteres del objeto recibido llamando a ezEncrypt.toCharArray() ; puede modificar el contenido de una matriz, por lo que tendrá que trabajar con eso, intercambiando los caracteres como se supone que debe hacerlo. Luego, una vez que se realiza el descifrado, cree otro objeto String utilizando el constructor new String(char[] chars) , pase su matriz como argumento y devuélvalo.

Algo más o menos así:

 static String ezDecrypt (String ezEncrypt){ //this variable holds the value of the String's length int cl = ezEncrypt.length(); //an array holding each character of the originally received text char[] chars = ezEncrypt.toCharArray(); //temporary space for a lonely character char tempChar; //Do your swapping here if (ezEncrypt.length() % 2 == 0){ //Length is even //for loop that begins at 0 //keeps looping until it reaches the end of the string //each loop adds 2 to the loop for(int i = 0; i < cl; i = i + 2) { tempChar = chars[i]; chars[i] = chars[i+1]; chars[i+1] = tempChar; } } else { //Length is odd //loop through and do the same process as above //except leave this loop will skip the last letter for(int i = 0; i < cl - 1; i = i + 2) { tempChar = chars[i]; chars[i] = chars[i+1]; chars[i+1] = tempChar; } } return new String(chars); }

Espero que esto te ayude.

over 4 years ago · Santiago Trujillo Relatório

0

Las cadenas son inmutables, por lo que llamar a un método en la cadena no cambiará la cadena. Solo devolverá un valor derivado de la cadena. Debe crear una nueva cadena vacía y comenzar a agregarle los valores de retorno carácter por carácter.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda