Home java Replacing opposite letters in the alphabet

Replacing opposite letters in the alphabet

Author

Date

Category

Task: Change letters in user input to opposite in the alphabet, it seems easy, the replace method to help, but when replacing letters to the opposite, they change back, for example: the line “abcdfgz”, – change “A” on “z” and “z” on “a”, respectively, should turn out: “zbcdfga” but it turns out: “abcdfga”, since with the second replacement already replaced the first time the letter changes in the second, how to make letters Changed to opposite in the alphabet, only 1 time?

Here is my code, I try only 2 letters there:

import java.util. *;
Public Class Program {
  Public Static Void Main (String [] Args) {
    Scanner Scan = New Scanner (System.in);
    String vvod = scan.nextline ();
    String res = vvod.replaceall ("a", "z"). ReplaceAll ("z", "a");
    System.Out.print (RES);
  }
}

Answer 1, Authority 100%

Each line in Java can be represented as an array of characters CHAR . Each CHAR character has a numeric code. Operating these numbers, you can change letters for any particular logic.

see The answer to the question of How to change the font without using methods like touppercase?

For this case:

Private Static String Replace (String Text) {
  char [] chararr = text.tocararray ();
  For (int i = 0; i & lt; chararr.length; i ++) {
    if (Chararr [i] & gt; = 'a' & amp; & amp; chararr [i] & lt; = 'z') {
      Chararr [I] = (char) ('z' - chararr [i] + 'a');
    } ELSE IF (Chararr [i] & gt; = 'a' & amp; & amp; chararr [i] & lt; = 'z') {
      Chararr [I] = (char) ('z' - chararr [i] + 'a');
    }
  }
  Return String.Valueof (Chararr);
}
Public Static Void Main (String [] Args) {
  String Str1 = "abcdefghijklmnopqrstuvwxyz";
  String Str2 = "abcdefghijklmnopqrstuvwxyz";
  STRING STR3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  System.out.printLN (STR1 + "|" + REPLACE (STR1));
  System.out.printLN (STR2 + "|" + REPLACE (STR2));
  System.Out.printLN (STR3 + "|" + REPLACE (STR3));
  // abcdefghijklmnopqrstuvwxyz | zyxwvutsrqponmlkjihgfedcba.
  // abcdefghijklmnopqrstuvwxyz | Zyxwvutsrqponmlkjihgfedcba.
  // abcdefghijklmnopqrstuvwxyz | zyxwvutsrqponmlkjihgfedcba.
  System.out.printLN (Replace (Replace (STR1)). Equals (STR1)); // True.
  System.Out.printLN (Replace (Replace (STR2)). Equals (STR2)); // True.
  System.out.printLN (Replace (Replace (STR3)). Equals (STR3)); // True.
}

Answer 2

public enum alphabet {
  a ('z')
  z ('a');
  Private Char Letter;
  AlphaBet (Char Letter) {
    this.letter = letter;
  }
  Public Char Getletter () {
    Return Letter;
  }
}
Import java.util.scanner;
Public Class Main {
  Public Static Void Main (String [] Args) {
    Scanner Scan = New Scanner (System.in);
    String vvod = scan.nextline ();
    char [] reverse = vvod.tocararray ();
    For (int i = 0; i & lt; reverse.length; i ++) {
      Reverse [i] = Alphabet.Valueof (String.Valueof (Reverse [i]). Getletter ();
    }
    System.out.PrintLN (New String (Reverse));
  }
}
Previous articleWhat is `\ U`?
Next articleerror in next.int ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions