문자열 함수 구현해보기

2015. 3. 10. 02:40엘키스공간/엘키스코딩공방

728x90
728x90


최대한 _s를 기준으로 구현해본다.


1. 문자열 길이함수

strlen 

원형/입출력





구현

1
2
3
4
5
6
size_t myStrlen( const char *str)
{
    const char* tail = str;
    while (*tail != '\0') { tail++; }
    return (size_t)(tail - str);
} 
cs


문자열 길이를 리턴하는 함수이다.

간단하게 포인터가 NULL까지 도달할때까지 뛰고 포인터 연산을 리턴하여 쉽게 구현할 수 있다.


strnlen_s 

원형/입출력




구현

1
2
3
4
5
6
7
8
9
10
11
12
size_t myStrnlen_s(const char *str, size_t numberOfElements )
{
    int i = 0;
    const char* tail = str;
    while (*tail != '\0'
    {
        if (numberOfElements < i) { return numberOfElements; }
        i++;
        tail++
    }
    return tail - str;
}
cs

_s의 차이점을 잘 몰라.. 일단 조건대로 구현했다.

max 버퍼를 정해놓고 이 보다 높은 문자열이 들어왔을시 max 버퍼를 리턴하는 방식.

기존 strlen 함수에서 길이 검사를 추가하면 된다.


2. 문자열 복사

strcpy_s

원형/입출력




구현

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
errno_t myStrcpy_s(char *strDestination, size_t numberOfElements, const char *strSource)
{
    if(strDestination == NULL)
    {
        printf("Error code  %d\n", EINVAL);
        return EINVAL;
    }
    else if (strSource  == NULL)
    {
        printf("Error code  %d , dest = NULL\n", EINVAL);
        strDestination = NULL;
        return EINVAL;
    }
    else if( numberOfElements <= 0)
    {
        printf("Error code %d , dest = NULL\n", ERANGE);
        strDestination = NULL;
        return ERANGE;
    } 
    
    while(*strSource != '\0') { *strDestination++ = *strSource++; }
    *strDestination = '\0';
 
    return 0;
}
cs


if문을 통해 dest, src의 NULL 포인터를 해결 했다.

길이가 0보다 커야하기 때문에 그것에 대한 에러도 해걸!

문자열이 복사될 때 널 문자열까지 포함되기때문에 line22에서 널을 넣어준다.


strncpy_s

원형/입출력




구현

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
errno_t myStrncpy_s(char *strDest, size_t numberOfElements, const char *strSource, size_t count)
{
    if(strDest == NULL)
    {
        printf("Error code  %d\n", EINVAL);
        return EINVAL;
    }
    else if (strSource  == NULL)
    {
        printf("Error code  %d , dest = NULL\n", EINVAL);
        strDest = NULL;
        return EINVAL;
    }
    else if( numberOfElements <= 0)
    {
        printf("Error code %d , dest = NULL\n", ERANGE);
        return ERANGE;
    }
    else if( numberOfElements < count)
    {
        printf("Error code %d , dest = NULL\n", EINVAL);
        strDest = NULL;
        return EINVAL;
    } 
    
    while(*strSource != '\0') { *strDest++ = *strSource++; }
    *strDest = '\0';
 
    return 0;
}
cs


strncpy_s와 다른점이 있다면 길이 검사를 한다는 것.


_strdup

형/입출력



구현

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char *my_Strdup(const char *strSource )
{
    int size = 0;
    char* retStr = 0;
    const char* tempSrc = strSource;
    char* tempRet = 0;
    
    while (*tempSrc++ != '\0') { size++; }
    
    retStr = (char*)malloc(sizeof(char)*size+1);
    tempRet = retStr;
 
    while(*strSource != '\0') { *tempRet++ = *strSource++; }
    *tempRet = '\0';
    
    return retStr;
}
cs


다른 문자열을 길이를 읽어 동적할당 char*를 반환하는 함수이다.

기본적으로 동적할당 + cpoy 형태로 간단히 구현했다.


3. 문자열 연결

strcat_s

원형/입출력


구현


strncat_s

원형/입출력


구현


4. 문자열 비교
strcmp

strncmp

_stricmp

_strnicmp

strcoll

5. 문자열 검색
strchr

strrchr

strstr

strpbrk

strcspn

strtok_s

6. 문자열 변환



728x90
반응형

'엘키스공간 > 엘키스코딩공방' 카테고리의 다른 글

큐로 메시지 큐 구현하기  (0) 2015.06.10
큐 구현하기  (0) 2015.06.10
[코딩] 스택 구현하기  (1) 2015.06.10
가위바위보 게임 만들기  (0) 2015.03.24
베스킨라빈스 31 게임  (0) 2015.03.24
발전하는 성적표 관리 시스템  (0) 2015.03.24
반복문  (0) 2015.03.23