blog.itcode.devblog.itcode.dev

[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 *.

[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 *.
RWB0104
@RWBwritten at 2021-12-18 12:52:30
Programmers

시리즈 모아보기

Programmers

61 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Masking a Phone Number

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_numberreturn
"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
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08