-2

I am trying to reverse the entire Alphabetical order A-Z --> Z-A.

This is what I have so far.

public class Program {
   public static voud main(String[] args) {

        String text = "Hi My name is Dave\n";

        text = text.replaceAll("[a-z ]",["z-a"]);


        System.out.println(text);

This will print out z-a for every letter, which is why I was wondering is there a way to replace every single character from a-z to z-a?

Parnak Dave
  • 1
  • 1
  • 3

4 Answers4

2

Actually, it's very difficult to develop it using RegEx. I suggest using Stream and Lambda:

    String text = "Hi My name is Dave\n";
    int[] chars = text.chars()
            .map(ch -> Character.isUpperCase(ch) ? 25 - ch + 'A' * 2 :
                    Character.isLowerCase(ch) ? 25 - ch + 'a' * 2 : ch)
            .toArray();
    text = new String(chars, 0, chars.length);
    System.out.println(text);

And the output is:

Sr Nb mznv rh Wzev
Mehdi Javan
  • 1,081
  • 6
  • 25
1

This is a solution in case you are not using Java 8 with streams and Lambdas.

If you want to reverse the order of the characters in your original string ("ABC" ==> "CBA"), try this code:

public static String reverseString(final String original) {
   StringBuffer reverse = new StringBuffer();

   for (int i = original.length() - 1 ; i >= 0 ; i-- )
      reverse.append(original.charAt(i));

   return reverse.toString();
}

If you want to replace "A" with "Z", "B" with "Y" and so on ("ABC" ==> "ZYX") try this code:

public static String reverseCharacters(final String original) {
   final int UPPERCASE_A = 'A';
   final int UPPERCASE_Z = 'Z';
   final int LOWERCASE_A = 'a';
   final int LOWERCASE_Z = 'z';

   StringBuffer reverse = new StringBuffer();
   char character = ' ';

   for ( int i = original.length() - 1 ; i >= 0 ; i-- ) {
      int charInt = original.charAt(i);
      if (Character.isUpperCase(original.charAt(i)) {
         reverse.append((char)(UPPERCASE_Z - charInt + UPPERCASE_A));
      } else if (Character.isUpperCase(original.charAt(i))) {
         reverse.append((char)(LOWERCASE_Z - charInt + LOWERCASE_A);
      } else {
         reverse.append(original.charAt(i));
      }
   }

   return reverse.toString();
}
Binyamin Regev
  • 914
  • 5
  • 19
  • 31
  • Hi thank your for the input but I really am looking to reverse the entire alphabetic order so when a user types in "ab" it outputs "zy". So every string reverse the output from z-->a – Parnak Dave Aug 15 '17 at 09:58
  • So `reverseCharacters` is the method to use if you are using Java 7 or earlier. If you are using Java 8 then @Mehdi Javan answer does what you want. – Binyamin Regev Aug 16 '17 at 08:54
0

split the string into an array of char, call sort, the build back up reversely into a string

    String text = "Hi My name is Dave\n";

    char arr [] = text.toCharArray();
    Arrays.sort(arr);

    for (int x = arr.length - 1; x >= 0; x--) {
        System.out.print(arr[x]);
    }
    System.out.print("<end>");

output

yvsnmiieeaaMHD    
<end>
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
-2
new StringBuilder(text).reverse().toString()
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311