-6

Given a string in Java, how can I obtain a new string where all adjacent sequences of digits are reversed?

My code:

import static java.lang.System.*;

public class P2
{
    public static void main(String[] args)
    {
        if(args.length < 1)
        {
            err.printf("Usage: java -ea P2 String [...]\n");
            exit(1);
        }

        String[] norm = new String[args.length];
        for(int i = 0; i<norm.length;i++)
        {
            norm[i] = args[i];
        }
    }

    public String invertDigits(String[] norm)
    {   

    }
}

And as an example, this is what it should do:

Inputs: 1234 abc9876cba a123 312asd a12b34c56d

1234 -> 4321

abc9876cba -> abc6789cba

a123 -> a321

312asd -> 213asd

a12b34c56d -> a21b43c65d

E_net4
  • 27,810
  • 13
  • 101
  • 139
mackdiogo
  • 3
  • 1
  • 5

3 Answers3

4

Although the question is heavily downvoted, the proposed problem seems clear now. I chose to solve it using a regular expression match in a recursive function.

private static String reverseDigits(String s) {
    // the pattern will match a sequence of 1 or more digits
    Matcher matcher = Pattern.compile("\\d+").matcher(s);
    // fetch the position of the next sequence of digits
    if (!matcher.find()) {
        return s; // no more digits
    }
    // keep everything before the number
    String pre = s.substring(0, matcher.start());
    // take the number and reverse it
    String number = matcher.group();
    number = new StringBuilder(number).reverse().toString();

    // continue with the rest of the string, then concat!
    return pre + number + reverseDigits(s.substring(matcher.end()));
}

And here's the iterative approach.

private static String reverseDigits(String s) {
    //if (s.isEmpty()) return s;
    String res = "";
    int base = 0;
    Matcher matcher = Pattern.compile("\\d+").matcher(s);
    while (!matcher.hitEnd()) {
        if (!matcher.find()) {
            return res + s.substring(base);
        }
        String pre = s.substring(base, matcher.start());
        base = matcher.end();
        String number = matcher.group();
        number = new StringBuilder(number).reverse().toString();
        res += pre + number;
    }
    return res;
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
0
    String str = "1234";

    //indexes
    int i = 0, j = str.length()-1;

    // find digits (if any)
    while (!Character.isDigit(str.charAt(i)) && i < str.length()) {
        i++;
    }
    while (!Character.isDigit(str.charAt(j)) && j >= 0) {
        j--;
    }

    // while we havent searched all the digits
    while (i < j) {
        // switch digits
        str = str.substring(0, i) + str.charAt(j) + str.substring(i + 1, j) + str.charAt(i) + str.substring(j + 1);
        i++;
        j--;
        // find the next digits
        while (!Character.isDigit(str.charAt(i)) && i < str.length()) {
            i++;
        }
        while (!Character.isDigit(str.charAt(j)) && j >= 0) {
            j--;
        }
    }
    System.out.println(str);
BlueMoon93
  • 2,910
  • 22
  • 39
0

Another dynamic approach without using regex classes:

public static String reverseOnlyNumbers(String s) {
    StringBuilder digits = new StringBuilder();
    StringBuilder result = new StringBuilder();

    boolean start = false;

    for (int i = 0; i < s.length(); i++) {
        Character c = s.charAt(i);

        if (Character.isDigit(c)) {
            start = true;
            digits.append(c);
        }else {
            start = false;
            if (digits.length() > 0) {
                result.append(digits.reverse().toString());
                digits = new StringBuilder();
            }
            result.append(c);
        }
    }

    return start ? result.append(digits.reverse()).toString() : result.toString();
}
aelkz
  • 1,786
  • 1
  • 24
  • 26