Notice
Recent Posts
Recent Comments
Tags
more
Today
Total
«   2025/11   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
관리 메뉴

SW 꿈나무

[Level 1][C] 핸드폰 번호 가리기 본문

Algorithm/Programmers

[Level 1][C] 핸드폰 번호 가리기

현식 :) 2020. 3. 26. 17:07
  • 문제

    전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자를 * 로 가린 문자열을 리턴 하는 함수, solution을 완성하세요.

  • 제한 조건

    s는 길이 4 이상, 20 이하인 문자열

  • Example

    Input 1 : "01033334444"
    Input 2 : "027778888"
    
    Output 1 : "*******4444"
    Output 2 : "*****8888"
  • Code

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    char* solution(const char* phone_number) {
    
        int length = strlen(phone_number);           // phone_number 의 길이 확인
        char* answer = (char*)malloc(length+1);      // 문자열 변수 answer에 NULL을 고려한 메모리 동적 할당
        strncpy(answer,phone_number,length);         // answer에 phone_number의 데이터 카피
    
        for(int i=0;i<length-4;i++) {                // 뒷 4자리를 제외한 모든 자리수에 
            answer[i]='*';                           // 데이터를 *로 변경
        }
        answer[length] = NULL;                       // 문자열 마지막 자리 NULL 대입
        return answer;                               // answer 반환
    }

'Algorithm > Programmers' 카테고리의 다른 글

[Level 1][C] 정수 제곱근 판별  (0) 2020.03.26
[Level 1][C] 짝수와 홀수  (0) 2020.03.26
[Level 1][C] 하샤드 수  (0) 2020.03.26
[Level 1][C] 평균 구하기  (0) 2020.03.26
[Level 1][C] 콜라츠 추측  (0) 2020.03.26
Comments