[Programmers / JAVA] Level 1 Masking a Phone Number (12948)
[Programmers / JAVA] Level 1 Masking a Phone Number (12948)
To protect personal information, Programmers Mobile masks part of a customer's phone number when sending a bill. Given the phone number as a string phone_number, complete the function solution that returns a string in which every digit except the last 4 digits of the phone number is replaced with the character *.
@RWBwritten at 2021-12-18 12:52:30
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
To protect personal information, Programmers Mobile masks part of a customer's phone number when sending a bill.
Given the phone number as a string phone_number, complete the function solution that returns a string in which every digit except the last 4 digits of the phone number is replaced with the character *.
- s is a string with a length between 4 and 20, inclusive.
| phone_number | return |
|---|---|
| "01033334444" | "*******4444" |
| "027778888" | "*****8888" |
We need to mask a phone number, leaving only the last 4 digits and replacing the rest with asterisks (*).
Since the goal is fairly straightforward, there's no need to use regular expressions. We can split the characters of phone_number, run a for loop, and mask each character up to 4 characters before the end of phone_number with *.
JAVA
/** * Masking a Phone Number class * * @author RWB * @since 2021.12.13 Mon 22:06:10 */ class Solution { /** * Method that returns the answer * * @param phone_number: [String] phone number * * @return [String] answer */ public String solution(String phone_number) { char[] chars = phone_number.toCharArray(); for (int i = 0; i < phone_number.length() - 4; i++) { chars[i] = '*'; } return new String(chars); } }
# Programmers# Algorithm# JAVA# Level 1
