ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문자열함수
    카테고리 없음 2013. 2. 20. 01:48
    반응형

    #include <iostream>
    #include <cstring> // #include <string.h>
    using namespace std;
    //< 문자열 길이 구하는 함수 
    //size_t  strlen( const char * _Str);
    // ( unsigned int ) == size_t
    //< 매개변수로 문자형 포인터 하나를 받아서 
    //< 길이를 반환한다.
    unsigned int Mystrlen( const char* pStr );
    //< 복사하는
    //char * strcpy(char* _Dest, const char *_Source);
    // int strcpy_s(char * _Dst,  unsigned int _SizeInBytes, const char * _Src);
    // strncpy( 용도 파악 )
    char* MyStrcpy( char* pDest, const char* pSrc );
    int MyStrcpy_s( char* pDest, unsigned int SizeinBytes , const char* pSrc );
    ////< 붙이는 함수
    //strcat
    //__DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1(char *, __RETURN_POLICY_DST, __EMPTY_DECLSPEC, strcat, _Pre_cap_for_(_Source) _Prepost_z_, char, _Dest, _In_z_ const char *, _Source)
    //strcat_s
    //__DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, strcat_s, _Deref_prepost_z_ char, _Dest, _In_z_ const char *, _Source)
    //// strncat (용도 파악 )
    char* MyStrcat( char* pDest , const char* pSrc );
    int MyStrcat_s( char* pDest , unsigned int SizeinBytes, const char* pSrc );
    //
    ////< 비교하는함수 
    //strcmp
    //_Check_return_ int     __cdecl strcmp(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
    //// strncmp ( 용도 파악 )
    int Mystrcmp(const char * _Str1, const char * _Str2);
    int main( void )
    {
    char* pTest = "test";
    char szBuf[20];// = { 'a', 'b','c','d'}; 
    //  for( int i = 0 ; i < 13 ; i++ )
    //  {
    //  szBuf[i] = 'a' + i;
    //  }
    cout << strlen(pTest) << endl;
    cout << Mystrlen(pTest) << endl;
    //< 복사 테스트
    cout << MyStrcpy(szBuf,pTest) << endl;
    cout << szBuf << endl;
    cout << MyStrcpy( szBuf , "테스트") << endl;
    // cout << strcpy(szBuf ,"흐흐") << endl;
    cout << strcpy_s( szBuf, 5, "흐흐" ) << endl;
    cout << MyStrcpy_s( szBuf , 5 , "히히" ) << endl;
    cout << szBuf << endl;
    //< 붙이기 테스트
    //cout << strcat( szBuf, "하하") << endl;
    //cout << strcat_s(szBuf , 20 ,"호호") << endl;
    cout << MyStrcat( szBuf , "하하") << endl;
    cout << MyStrcat_s( szBuf , 13,"호호") << endl;
    cout << szBuf << endl;
    return 0;
    }
    //< 길이를 반환한다.
    unsigned int Mystrlen( const char* pStr )
    {
    /-int i;
    for( i = 0 ; pStr[i] != NULL ; i++ ) { }*-
    const char* pDest = pStr;
    while( *pDest++ != NULL  ) pDest++; 
    return pDest - pStr - 1;
    }
    //< 문자열 복사 함수 
    char* MyStrcpy( char* pDest, const char* pSrc )
    {
    //< 배열 버전
    int i;
    for( i = 0 ; pSrc[i] != NULL ; i++ )
    {
    pDest[i] = pSrc[i];
    }
    //< 
    pDest[i] = NULL;
    //pDest[i] = '\0';
    //pDest[i] = 0;
    //< 포인터 버전 
    return pDest;
    }
    int MyStrcpy_s( char* pDest, unsigned int SizeinBytes , const char* pSrc )
    {
    //< 포인터 널체크
    if( pDest == NULL || pSrc == NULL )
    {
    if( pDest == NULL )
    {
    cout <<"pDest가 NULL이다 시키야" << endl;
    }
    else
    {
    cout <<"pSrc가 NULL이다 시키야" << endl;
    }
    //< 에러 처리 
    //cout << "이시키가!!! 장난하냐!!" << endl;
    return -1;
    }
    //< 크기 체크
    unsigned int nDestLen = Mystrlen(pDest);
    unsigned int nSrcLen = Mystrlen(pSrc);
    // DEST는 최소한 SRC보다 커야한다. ( 같거나 )
    if( SizeinBytes < nSrcLen + 1 )
    {
    cout <<"너무 작다!!" << endl;
    return -1;
    }
    for( unsigned int i = 0 ; i < nSrcLen ;i++ )
    {
    pDest[i] = pSrc[i];
    }
    //< NULL~~
    pDest[nSrcLen] = NULL;
    return 0;
    }
    //< 붙이기
    char* MyStrcat( char* pDest , const char* pSrc )
    {
    int nDestLen = Mystrlen(pDest);
    int nSrcLen = Mystrlen(pSrc);
    for( int i = 0 ; i < nSrcLen ; i++ )
    {
    pDest[nDestLen + i] = pSrc[i];
    }
    pDest[nDestLen + nSrcLen] = NULL;
    return pDest;
    }
    int MyStrcat_s( char* pDest , unsigned int SizeinBytes, const char* pSrc )
    {
    //< 포인터 널체크
    if( pDest == NULL || pSrc == NULL )
    {
    if( pDest == NULL )
    {
    cout <<"pDest가 NULL이다 시키야" << endl;
    }
    else
    {
    cout <<"pSrc가 NULL이다 시키야" << endl;
    }
    //< 에러 처리 
    //cout << "이시키가!!! 장난하냐!!" << endl;
    return -1;
    }
    //< 크기 체크
    unsigned int nDestLen = Mystrlen(pDest);
    unsigned int nSrcLen = Mystrlen(pSrc);
    // DEST는 최소한 SRC보다 커야한다. ( 같거나 )
    if( SizeinBytes < nDestLen + nSrcLen + 1 )
    {
    cout <<"너무 작다!!" << endl;
    return -1;
    }
    for( unsigned int i = 0 ; i < nSrcLen ; i++ )
    {
    pDest[nDestLen + i] = pSrc[i];
    }
    pDest[nDestLen + nSrcLen] = NULL;
    return 0;
    }
    // 문자열 같은지 검사  : 왼쪽이 오른쪽보다 크면 양수
    //int strcmp(const char * _Str1, const char * _Str2);
    int Mystrcmp(const char * _Str1, const char * _Str2)
    {
    unsigned int nDestLen = Mystrlen(_Str1);
    unsigned int nSrcLen = Mystrlen(_Str2);
    // 길이가 다르다면 다른 문자열이다.
    if( nDestLen != nSrcLen )
    {
    int nSmall = min( nDestLen , nSrcLen );
    for( int i =  0 ;i  < nSmall ;i++)
    {
    if( _Str1[i] < _Str2[i])
    {
    return -1;
    }
    }
    return 1;
    }
    ///< 두수가 같을때
    for( int i = 0 ; i < (int)nDestLen ; i++ )
    {
    if( _Str1[i] != _Str2[i])
    {
    if( _Str1[i] < _Str2[i])
    {
    return -1;
    }
    else
    {
    return 1;
    }
    }
    }
    // 여기까지 아니면 같다!
    return 0;
    }

    반응형
Designed by Tistory.