-3

So it should work like that

squareDigits("a22b") on "a44b"

squareDigits("a9b2") on "a81b4"

What I have got so far :

public class Code {
    public static void main(String[] args) {
        String ok = "a2b9";
        System.out.println(squareDigits(ok)); // "a81b4"
    }

  public static String squareDigits(String s) {
        char[] pena = s.toCharArray();
        int aboba;
        for (char c : pena) {
            if (Character.isDigit(c)) {
                String s1 = Character.toString(c);
                int b = Integer.parseInt(s1);
                aboba = b * b;
                System.out.println(aboba);
            }
        }
        return "";
        }
    }

Im only beginner and this would very help me out

Stultuske
  • 9,296
  • 1
  • 25
  • 37

4 Answers4

0

You can take a StringBuffer and iterate over char array and check if char is digit then square it and put into string buffer and if not char put it as it is in string buffer.

     public static void main(String[] args) {
        System.out.println(squareDigits("a22b"));
        System.out.println(squareDigits("a9b2"));
    }

    public static String squareDigits(String s) {
        char[] pena = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char c : pena) {
            if (Character.isDigit(c)) {
                String s1 = Character.toString(c);
                int b = Integer.parseInt(s1);
                sb.append(b * b);
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
0

You can easily do it using StringBuilder like this:

public static void main(String[] args) {
    System.out.println(squareDigits("a22b")); // "a44b"
    System.out.println(squareDigits("a2b9")); // "a4b81"
    System.out.println(squareDigits("a55b")); // "a2525b"
}

public static String squareDigits(String s) {
    StringBuilder result = new StringBuilder();
    s.chars().forEach(c -> result.append(addDigitSquaredIfDigit(c)));

    return result.toString();
}

private static String addDigitSquaredIfDigit(int charAsDigit) {
    char c = (char) charAsDigit;
    return Character.isDigit(c) ? getSquaredDigitString(c) : String.valueOf(c);
}

private static String getSquaredDigitString(char c) {
    int digit = Integer.parseInt(Character.toString(c));

    return Integer.toString(digit * digit);
}
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
0

try as well.

public static void main(String[] args) {
    String ok = "a2b9";
    System.out.println(squareDigits(ok)); // "a81b4"
}

public static String squareDigits(String s) {
    StringBuilder sb = new StringBuilder();
    String[] chars = s.split("");
    for (String str : chars){
        if (isNumber(str)){
            int num = Integer.valueOf(str);
            Double square = Math.pow(num, 2);
            sb.append(square.intValue());
        } else {
            sb.append(str);
        }
    }
    return sb.toString();
}

private static boolean isNumber(String str) {
    try {
        Integer.valueOf(str);
        return true;
    } catch (Exception e) {
        return false;
    }
}
krishna thota
  • 182
  • 1
  • 3
  • 8
0

A purely functional way (maybe not the most optimal one):

public static String squareDigits(String s) {
      char[] pena = s.toCharArray();
      return IntStream.range(0, pena.length).mapToObj(i -> pena[i]).map(it -> {
        String c = it.toString();
        try {
          int intVal = Integer.parseInt(c);
          return "" + (intVal * intVal);
        } catch (Exception e) {
          return c;
        }
      }).reduce("", String::concat);
}
Tarmo
  • 3,851
  • 2
  • 24
  • 41