[프로그래머스 | Lv.1] 핸드폰 번호 가리기

2024. 3. 12. 18:49·Data structure & Algorithm/프로그래머스

 

문제 설명

 

프로그래머스 모바일은 개인정보 보호를 위해 고지서를 보낼 때 고객들의 전화번호의 일부를 가립니다.

전화번호가 문자열 phone_number로 주어졌을 때,

전화번호의 뒷 4자리를 제외한 나머지 숫자를 전부 *으로 가린 문자열을 리턴하는 함수,

solution을 완성해주세요.

 

 

제한 사항

 

phone_number는 길이 4 이상, 20이하인 문자열입니다.

 

 

입출력 예

 

phone_number return
"01033334444" "*******4444"
"027778888" "*****8888"

 

 

 

나의 풀이

func solution(_ phone_number:String) -> String {
    var answer = ""
    
    for i in 0...phone_number.count-1 {
        if i < phone_number.count-4 {
            answer += "*"
        } else {
            answer += String(phone_number[phone_number.index(phone_number.startIndex, offsetBy: i)])
        }
    }
    
    return answer
}

 

 

 

'Data structure & Algorithm > 프로그래머스' 카테고리의 다른 글

[프로그래머스 | Lv.1] 내적  (2) 2024.03.12
[프로그래머스 | Lv.1] 제일 작은 수 제거하기  (0) 2024.03.12
[프로그래머스 | Lv.1] 나누어 떨어지는 숫자 배열  (0) 2024.03.11
[프로그래머스 | Lv.1] 없는 숫자 더하기  (0) 2024.03.11
[프로그래머스 | Lv.1] 콜라츠 추측  (0) 2024.03.11
'Data structure & Algorithm/프로그래머스' 카테고리의 다른 글
  • [프로그래머스 | Lv.1] 내적
  • [프로그래머스 | Lv.1] 제일 작은 수 제거하기
  • [프로그래머스 | Lv.1] 나누어 떨어지는 숫자 배열
  • [프로그래머스 | Lv.1] 없는 숫자 더하기
ohhoooo
ohhoooo
  • ohhoooo
    ohhoooo
    ohhoooo
  • 전체
    오늘
    어제
    • 전체 글 (84)
      • iOS (1)
      • Swift (1)
      • Data structure & Algorithm (67)
        • 프로그래머스 (67)
      • 기타 (15)
        • TIL (2)
        • Android (13)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
ohhoooo
[프로그래머스 | Lv.1] 핸드폰 번호 가리기
상단으로

티스토리툴바