본문 바로가기
프로그래머스/C

[프로그래머스/C]프로그래머스 Level 1 : 핸드폰 번호 가리기 C언어

by starfish22 2021. 12. 24.
728x90

▶문제 : 코딩 테스트 연습 - 핸드폰 번호 가리기 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 핸드폰 번호 가리기

프로그래머스 모바일은 개인정보 보호를 위해 고지서를 보낼 때 고객들의 전화번호의 일부를 가립니다. 전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자

programmers.co.kr

 

▶코드 작성

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char *solution(const char *phone_number)
{
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    char *answer = (char *)malloc(strlen(phone_number) + 1);
    strcpy(answer, phone_number);//문자열 복사
    for (int i = 0; i < strlen(phone_number) - 4; i++)//문자길이에서 -4한 값까지 반복
    {
        answer[i] = '*';//별로 바꾸기
    }
    return answer;
}

 

▶해석

동적할당한 answer포인터에 phone_number문자열을 복사하여 문자 길이-4만큼의 값까지 i를 ++반복하고 별로 바꿔준다.

728x90

댓글