-2

How to find out if the char in string is a letter or a number?

I.e I have a string "abc2e4", I need to find the ints, square them, and put the answer back in the string (no extra operations with the letters), so the new string would be "abc4e16".

Im incredibly lost with this exercise, so any help would be great :D

noob
  • 1
  • 1
    [String#toCharAray](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toCharArray--) and [Character.isDigit](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isDigit-char-) will help – jmj Feb 07 '19 at 18:37
  • `"abc2e4".replaceAll("\\D","")` will leave you just with digits in the string. – ernest_k Feb 07 '19 at 18:38
  • 3
    @JigarJoshi there is no point in using `toCharArray`, because the new string may be longer than the old. `charAt` and a `StringBuilder` would be more appropriate. – Andy Turner Feb 07 '19 at 18:39
  • @Andy good point. – jmj Feb 07 '19 at 18:40

3 Answers3

0

Java provides a method to check whether a character is a digit. For this you can use Character.isDigit(char).

public static String squareNumbers(String input) {
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i); // get char at index
        if (Character.isDigit(c))  // check if the char is a digit between 0-9
            output.append((int) Math.pow(Character.digit(c, 10), 2)); // square the numerical value
        else
            output.append(c); // keep if not a digit
    }
    return output.toString();
}

This will iterate any passed string character by character and square each digit it finds. If for example 2 digits are right next to each other they will be seen as individual numbers and squared each and not as one number with multiple digits.

squareNumbers("10") -> "10"

squareNumbers("12") -> "14"

squareNumbers("abc2e4") -> "abc4e16"

Minn
  • 5,688
  • 2
  • 15
  • 42
0

You can do it using Regular Expression

public static String update(String str) {
    final Pattern pattern = Pattern.compile("\\D+|\\d+");
    final Matcher matcher = pattern.matcher(str);
    StringBuilder buf = new StringBuilder();
    int pos = 0;

    while (matcher.find(pos)) {
        str = matcher.group();
        buf.append(Character.isDigit(str.charAt(0)) ? (int)Math.pow(Integer.parseInt(str), 2) : str);
        pos = matcher.end();
    }

    return buf.toString();
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
-1

My logic only squares single digit numbers.

For eg - if you provide input he13llo, the output would be he19llo and not he169llo.

 Scanner in = new Scanner(System.in) ; 
    String str = in.next() ; 
    String ans = str ; 

    for (int i = 0 ; i < str.length() ; i++)
    {
        char ch = str.charAt(i) ; 
        if((ch - '0' >= 0) && (ch - '9' <= 0))
        {
            int index = i ; 
            int num = ch - '0' ; 
            int square = num * num ; 
            ans = ans.substring(0 ,index) + square  + ans.substring(index+1) ; 
        }
    }
    System.out.println(ans) ; 
 }
shellym
  • 546
  • 1
  • 5
  • 11