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

๐Ÿ—ฃ๏ธ ์‹ ์ž… ์ธํ„ฐ๋ทฐ/C++

์‹ ์ž… ๊ฐœ๋ฐœ์ž ๊ธฐ์ˆ ๋ฉด์ ‘ C++ : strcpy, strcat, strlen, atoI

strcpy ํ•จ์ˆ˜๋Š” ํ•œ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๋Œ€์ƒ ๋ฌธ์ž์—ด์ด ์ถฉ๋ถ„ํ•œ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋ฉฐ, ๋์— NULL ๋ฌธ์ž (\0) ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

char* StrCpy(char* dest, const char* src){
    char* ret = dest;
        
    while(*src){
        *dest = *src;
        dest++;
        src++;
    }
	*dest = '\0';
    return ret;
}

 

strcat ํ•จ์ˆ˜๋Š” ํ•œ ๋ฌธ์ž์—ด์˜ ๋์— ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ๋ถ™์ž…๋‹ˆ๋‹ค.  ์ด ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ์—๋„ ๋Œ€์ƒ ๋ฌธ์ž์—ด์— ์ถฉ๋ถ„ํ•œ ๊ณต๊ฐ„์ด ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์˜ ๋์—์„œ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

char* StrCat(char* dest, const char* src){
    chart* ret = dest;
    
    while(*dest){
        dest++; //dest์˜ ๋๊นŒ์ง€ ์ด๋™
    }
    
    while(*src){ //src ๋‚ด์šฉ์„ dest์˜ ๋์— ๋ณต์‚ฌ
        *dest = *src;
        dest++;
        src++;
    }
    
    return ret; //๋งˆ์ง€๋ง‰์— null ๋ฌธ์ž ์ถ”๊ฐ€
}

 

strlen ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋„ ์ข…๋ฃŒ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

int StrLen(const char *str){
    int count = 0;
    while(str[count] != 0)
        count++;
    return count;
}

 

atol (ASCII to long) ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์„ long ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. C++์—์„œ ์ด ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ๋ฌธ์ž๋ฅผ ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์ด๋“ค์„ ํ•ฉ์ณ ์ตœ์ข…์ ์ธ ์ˆซ์ž๋ฅผ ๋งŒ๋“ค์–ด๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ตฌํ˜„ ์‹œ ์ฃผ์˜ํ•  ์ ์€ ๊ณต๋ฐฑ ์ฒ˜๋ฆฌ, ๋ถ€ํ˜ธ ์ฒ˜๋ฆฌ, ๊ทธ๋ฆฌ๊ณ  ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋‚˜ ์œ ํšจํ•˜์ง€ ์•Š์€ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ์ž…๋‹ˆ๋‹ค.

  1. ๋จผ์ €, ๊ณต๋ฐฑ ๋ฌธ์ž๋ฅผ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.
  2. ๋‹ค์Œ์œผ๋กœ ๋ถ€ํ˜ธ๋ฅผ ํ™•์ธํ•˜๊ณ , ํ•ด๋‹นํ•˜๋Š” ๊ฒฝ์šฐ ๋‚ด๋ถ€ sign ๋ณ€์ˆ˜๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ฌธ์ž์—ด์˜ ๊ฐ ์ˆซ์ž ๋ฌธ์ž๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ, ๊ฐ ์ˆซ์ž๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์ด์ „ ๊ฐ’์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  4. ๋ณ€ํ™˜ ๊ณผ์ •์—์„œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์œผ๋ฉด, ์ด๋ฅผ ์ฒดํฌํ•˜๊ณ  ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  5. ์ตœ์ข… ๊ฒฐ๊ณผ์— ๋ถ€ํ˜ธ๋ฅผ ๊ณฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
#include <iostream>
#include <climits> // LONG_MAX์™€ LONG_MIN์„ ์œ„ํ•ด

long myAtol(const char *str) {
    long result = 0;
    int sign = 1;  // ๋ถ€ํ˜ธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜, ๊ธฐ๋ณธ์ ์œผ๋กœ ์–‘์ˆ˜

    // ๊ณต๋ฐฑ ๋ฌด์‹œ
    while (*str == ' ' || *str == '\t' || *str == '\n') {
        str++;
    }

    // ๋ถ€ํ˜ธ ์ฒดํฌ
    if (*str == '-' || *str == '+') {
        sign = (*str == '-') ? -1 : 1;
        str++;
    }

    // ์ˆซ์ž ๋ณ€ํ™˜
    while (*str >= '0' && *str <= '9') {
        int digit = *str - '0';

        // ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ์ฒดํฌ
        if (result > LONG_MAX / 10 || (result == LONG_MAX / 10 && digit > LONG_MAX % 10)) {
            return (sign == 1) ? LONG_MAX : LONG_MIN;
        }

        result = result * 10 + digit;
        str++;
    }

    return result * sign;
}

int main() {
    const char *str = "12345";
    long val = myAtol(str);
    std::cout << "The converted value is " << val << std::endl;
    return 0;
}