๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[Algorithm] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.1 : ํœด๋Œ€ํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ

by ์ฝ”๋”ฉ๊ณต์ฑ… 2022. 11. 1.
๋ฐ˜์‘ํ˜•

ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ชจ๋ฐ”์ผ์€ ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ๊ณ ์ง€์„œ๋ฅผ ๋ณด๋‚ผ ๋•Œ ๊ณ ๊ฐ๋“ค์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค. ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด phone_number๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋’ท 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ ์ „๋ถ€ " * "์œผ๋กœ ๊ฐ€๋ฆฐ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์‹œ์˜ค.


์ œํ•œ ์กฐ๊ฑด

phone_number๋Š” ๊ธธ์ด 4 ์ด์ƒ, 20์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

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

๋ฌธ์ œ ํ’€์ด

repeat()๊ณผ slice()๋ฅผ ์ด์šฉํ•œ ํ’€์ด

let phone_number = "01033334444";

function solution(phone_number) {
    var answer = '';
    answer = "*".repeat(phone_number.length -4)+ phone_number.slice(-4);
    return answer;
}
solution(phone_number);
}

๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ์‹

์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์Šค์ฟจ

#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(sizeof(char) * strlen(phone_number) + 1);
    char* star = "****************";

    strcpy(answer, phone_number);
    strncpy(answer, star, strlen(phone_number) - 4);

    return answer;
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

JavaScript
HTML
CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.